转:http://stackoverflow/questions/202302/rounding-to-an-arbitrary-number-of-significant-digits
Q:
How can you round any number (not just integers > 0) to N significant digits?
For example, if I want to round to 3 significant digits, I'm looking for a formula that could take:
1,239,451 and return 1,240,000
12.1257 and return 12.1
.0681 and return .0681
5 and return 5
Naturally the algorithm should not be hard-coded to only handle N of 3, although that would be a start.
A:
15 Answers
active oldest votesup vote 82 down vote |
Here's the same code in Java without the 12.100000000000001 bug other answers have I also removed repeated code, changed The bug was caused by multiplying a large number with a small number. Instead I divide two numbers of similar size. EDIT
| ||||||||||||
|
up vote 15 down vote |
SUMMARY:
So you need to find the decimal place of the first non-zero digit, then save the next N-1 digits, then round the Nth digit based on the rest. We can use log to do the first.
So for numbers > 0, take the ceil of the log. For numbers < 0, take the floor of the log. Now we have the digit We have to round the
Then do the standard rounding thing:
And undo the pow.
Where power is the power calculated above.
About accuracy: Pyrolistical's answer is indeed closer to the real result. But note that you can't represent 12.1 exactly in any case. If you print the answers as follows:
The answers are:
So, use Pyro's answer!
| ||||||||||||||||||||
|
up vote 10 down vote |
Here's a short and sweet JavaScript implementation:
| ||||||||||||||||
|
up vote 10 down vote |
Isn't the "short and sweet" JavaScript implementation
e.g.
? Sorry, I'm not being facetious here, it's just that using the "roundit" function from Claudiu and the .toPrecision in JavaScript gives me different results but only in the rounding of the last digit. JavaScript:
.NET
| ||||||||
|
up vote 6 down vote |
Pyrolistical's (very nice!) solution still has an issue. The maximum double value in Java is on the order of 10^308, while the minimum value is on the order of 10^-324. Therefore, you can run into trouble when applying the function
then the variable
| ||||
|
up vote 5 down vote |
How about this java solution : double roundToSignificantFigure(double num, int precision){ return new BigDecimal(num) .round(new MathContext(precision, RoundingMode.HALF_EVEN)) .doubleValue(); }
| ||||
|
up vote 3 down vote |
Here is a modified version of Ates' JavaScript that handles negative numbers.
| ||
add a comment |
up vote 1 down vote |
Have you tried just coding it up the way you'd do it by hand?
| ||
add a comment |
up vote 1 down vote |
[Corrected, 2009-10-26] Essentially, for N significant fractional digits: • Multiply the number by 10N For N significant integral (non-fractional) digits: • Divide the number by 10N You can do this on any calculator, for example, that has an "INT" (integer truncation) operator.
| ||||||||
|
up vote 1 down vote |
| ||
add a comment |
up vote 1 down vote |
Here is Pyrolistical's (currently top answer) code in Visual Basic.NET, should anyone need it:
| ||
add a comment |
up vote 1 down vote |
This came 5 years late, but though I'll share for others still having the same issue. I like it because it's simple and no calculations on the code side. See Built in methods for displaying Significant figures for more info. This is if you just want to print it out.
This is if you want to convert it:
Here's an example of it in action:
| ||
add a comment |
up vote 0 down vote |
This is one that I came up with in VB:
| ||
add a comment |
up vote 0 down vote |
JavaScript:
The
| ||
add a comment |
up vote -1 down vote |
This code uses the inbuilt formatting function which is turned to a rounding function |
更多推荐
rounding to an arbitrary number of significant digits
发布评论