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:
Isn't the "short and sweet" JavaScript implementation
Number(n).toPrecision(sig)
e.g.
alert(Number(12345).toPrecision(3)
?
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.
Here's the same code in Java without the 12.100000000000001 bug other answers have
I also removed repeated code, changed power to a type integer to prevent floating issues when n - d is done, and made the long intermediate more clear
The bug was caused by multiplying a large number with a small number. Instead I divide two numbers of similar size.
EDIT
Fixed more bugs. Added check for 0 as it would result in NaN. Made the function actually work with negative numbers (The original code doesn't handle negative numbers because a log of a negative number is a complex number)
public static double roundToSignificantFigures(double num, int n) {
if(num == 0) {
return 0;
}
final double d = Math.ceil(Math.log10(num < 0 ? -num: num));
final int power = n - (int) d;
final double magnitude = Math.pow(10, power);
final long shifted = Math.round(num*magnitude);
return shifted/magnitude;
}
/**
* Set Significant Digits.
* @param value value
* @param digits digits
* @return
*/
public static BigDecimal setSignificantDigits(BigDecimal value, int digits) {
//# Start with the leftmost non-zero digit (e.g. the "1" in 1200, or the "2" in 0.0256).
//# Keep n digits. Replace the rest with zeros.
//# Round up by one if appropriate.
int p = value.precision();
int s = value.scale();
if (p < digits) {
value = value.setScale(s + digits - p); //, RoundingMode.HALF_UP
}
value = value.movePointRight(s).movePointLeft(p - digits).setScale(0, RoundingMode.HALF_UP)
.movePointRight(p - digits).movePointLeft(s);
s = (s > (p - digits)) ? (s - (p - digits)) : 0;
return value.setScale(s);
}
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 roundToSignificantFigures to something that's within a few powers of ten of Double.MIN_VALUE. For example, when you call
roundToSignificantFigures(1.234E-310, 3);
then the variable power will have the value 3 - (-309) = 312. Consequently, the variable magnitude will become Infinity, and it's all garbage from then on out. Fortunately, this is not an insurmountable problem: it is only the factormagnitude that's overflowing. What really matters is the productnum * magnitude, and that does not overflow. One way of resolving this is by breaking up the multiplication by the factor magintude into two steps:
public static double roundToNumberOfSignificantDigits(double num, int n) {
final double maxPowerOfTen = Math.floor(Math.log10(Double.MAX_VALUE));
if(num == 0) {
return 0;
}
final double d = Math.ceil(Math.log10(num < 0 ? -num: num));
final int power = n - (int) d;
double firstMagnitudeFactor = 1.0;
double secondMagnitudeFactor = 1.0;
if (power > maxPowerOfTen) {
firstMagnitudeFactor = Math.pow(10.0, maxPowerOfTen);
secondMagnitudeFactor = Math.pow(10.0, (double) power - maxPowerOfTen);
} else {
firstMagnitudeFactor = Math.pow(10.0, (double) power);
}
double toBeRounded = num * firstMagnitudeFactor;
toBeRounded *= secondMagnitudeFactor;
final long shifted = Math.round(toBeRounded);
double rounded = ((double) shifted) / firstMagnitudeFactor;
rounded /= secondMagnitudeFactor;
return rounded;
}
Here is Pyrolistical's (currently top answer) code in Visual Basic.NET, should anyone need it:
Public Shared Function roundToSignificantDigits(ByVal num As Double, ByVal n As Integer) As Double
If (num = 0) Then
Return 0
End If
Dim d As Double = Math.Ceiling(Math.Log10(If(num < 0, -num, num)))
Dim power As Integer = n - CInt(d)
Dim magnitude As Double = Math.Pow(10, power)
Dim shifted As Double = Math.Round(num * magnitude)
Return shifted / magnitude
End Function
Function SF(n As Double, SigFigs As Integer)
Dim l As Integer = n.ToString.Length
n = n / 10 ^ (l - SigFigs)
n = Math.Round(n)
n = n * 10 ^ (l - SigFigs)
Return n
End Function
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.
public String toSignificantFiguresString(BigDecimal bd, int significantFigures){
return String.format("%."+significantFigures+"G", bd);
}
This is if you want to convert it:
public BigDecimal toSignificantFigures(BigDecimal bd, int significantFigures){
String s = String.format("%."+significantFigures+"G", bd);
BigDecimal result = new BigDecimal(s);
return result;
}
I needed this in Go, which was a bit complicated by the Go standard library's lack of math.Round() (before go1.10). So I had to whip that up too. Here is my translation of Pyrolistical's excellent answer:
// TODO: replace in go1.10 with math.Round()
func round(x float64) float64 {
return float64(int64(x + 0.5))
}
// SignificantDigits rounds a float64 to digits significant digits.
// Translated from Java at https://stackoverflow.com/a/1581007/1068283
func SignificantDigits(x float64, digits int) float64 {
if x == 0 {
return 0
}
power := digits - int(math.Ceil(math.Log10(math.Abs(x))))
magnitude := math.Pow(10, float64(power))
shifted := round(x * magnitude)
return shifted / magnitude
}