commons-lang icon indicating copy to clipboard operation
commons-lang copied to clipboard

NumberUtils countSignificantFigures method

Open theshoeshiner opened this issue 2 years ago • 2 comments

A util method in NumberUtils that counts the number of significant figures in a decimal strings of standard / exponential format. e.g.(101, 102.1, 5.1e2, 1.001, 0.2e4, etc...)

This method counts the number of "significant figures" according to standard mathematical rules (details and references are in the javadoc). It must operate on a string because the rules themselves inherently depend on the string representation and not the underlying numeric value. The rules are well documented and widely accepted, aside from zero values, which are not usually covered by the conventions and the choice as to how to interpret is documented in the javadoc.

My first instinct was that this belonged in commons-math or commons-numbers, as the concept of significant figures is usually necessary in scientific / mathematical fields, but thus far it doesnt seem to be a good fit. Decided Id create the PR here so that at least the implementation is out there for review and perhaps this discussion will find it a better home. I do feel like this is a valuable feature because any manipulation of numerical scientific data that needs to be accurate (e.g. clinical data) ultimately needs to respect significant figure counts, which are not tracked by the standard java Number classes.

There are 7 basic rules that the method follows, No. 1-5 being the core sigfig rules that you'll find on most external sources, No. 6 to explain how/why zero values are handled, and No. 7 to explain how exponents are handled (or excluded, rather). The overall format of the string is validated, with IAEs thrown for invalid characters or formatting.

Examples of the results:

String Sig Figs
1.00 3
0.0045 2
13400 3
0.0 2
0.00 2
1.2000 5
3.24e10 3
4.110E-3 4

theshoeshiner avatar Aug 14 '23 15:08 theshoeshiner

This cannot parse standard floating point numbers generated by String valueOf as the exponent character in that case is E.

What if the exponent is negative?

What if the number is negative?

What if either number or exponent have an explicit + sign?

What if the number has more than 1 decimal point?

aherbert avatar Aug 14 '23 15:08 aherbert

@aherbert

This cannot parse standard floating point numbers generated by String valueOf as the exponent character in that case is E. What if the exponent is negative? What if the number is negative? What if either number or exponent have an explicit + sign?

All good points. Code should be fixed to handle all of these successfully. Although an explicit '+' might debatably be an error case, I would think it's better to handle it and document that it's allowed.

What if the number has more than 1 decimal point?

Code should be fixed to fail on this case.

theshoeshiner avatar Aug 14 '23 15:08 theshoeshiner

Doesn't this belong in Commons Math?

garydgregory avatar Sep 18 '25 01:09 garydgregory