Every once in a while I have a need to report monitoring results using a specific number of significant figures. For example, a project manager may ask me to show all results on a map with 2 significant figures. So:
0.358 would be 0.36
4.2 would be 4.2
45681 would be 46000
0.02 would be 0.020
This is always tricky, especially when the number has trailing zeros. Increasingly, I’m using Python instead of VBScript to form label expressions in ArcGIS. I’ve found that I can frequently do pretty cool things with the Python interpreter in the label expression builder, although sometimes I have Python code that works fine in a stand-alone script that won’t work inside the label expression. The debugging inside the label expression dialog box is terrible, so it’s often hard to track down what’s not working when things go awry.
Over at Randle Taylor’s blog, I found a great Python function for formating a number to a specific number of significant figures. I’ve taken Randle’s code and combined it with some ArcGIS specific label function logic. In this particular case, I have three fields that I’m using the drive the labels:
[RESULT_VAL] is the analytical result;
[MDL] is the detection limit. I want to use this when the sample results are below the detection limit;
[RESUT_QUA] is the field that holds lab reporting flags.
I added some additional functionality to this so that really large numbers are returned using scientific notation. The number of significant figures to use is specified in the initial FindLabel function in the call to the Helper function (next to the last line of the FindLabel function. The threshold for when scientific notation is used is set in the helper function (10,000). Hopefully someone finds this useful.