Results 1 to 3 of 3
 10272008, 02:50 PM #1Member
 Join Date
 Oct 2008
 Location
 The Netherlands
 Posts
 2
 Rep Power
 0
rounding a double in a number of significant numbers
After calulating some number i need to round the result into a "variable" (only 2 or 3) number of significant figures.
e.g.
0.00245 2 > 0.0025 3 > 0.00245
5141.0 2 > 5100.0 3 > 5140.0
1.0202 2 > 1.0 3 > 1.02
I couldn't found a standard solution in the JAVA API. I tried the Decimal formatter but this only rounds to a number of figures behind the . (I don't know the exact english term but 0.000022115 is shown as 0.00 instead of 0.000022).
There is a mathematical formula for rounding. I implemented as following in my code:
static public Double roundDouble(Double l_value, Integer a_NumberOfSignificantFigures)
{
Double l_roundedvalue = null;
if (l_value != null && a_NumberOfSignificantFigures > 0 && l_value > 0.0)
{
// this truncates all irrelevant numbers
double l_exp = Math.floor((Math.log10(l_value) a_NumberOfSignificantFigures + 1));
double l_power = Math.pow(10, l_exp);
l_roundedvalue = Math.floor((l_value/l_power) + 0.5) * l_power;
}
else
{
// if rounding is not possible return original result
l_roundedvalue = l_value;
}
return l_roundedvalue;
}
To display the rounded values I use the toString() method.
The result is that in most cases the result is as expected but for some there result is shown as:
250.000000000002 or 0.00000098000004.
I am aware of the fact that this happens with floating point precision.
However I'm looking for a solution to print the two examples above as 250.0 and 00000098 (in case of 2 significant figures). I don't want to use the scientific notation.
The only solution I can think of is to write my own string formatter to display the doubles correct. However I cannot imagine there isn't a standard java solution to this problem.
Any help is appreciated.
Rik
 10272008, 03:26 PM #2Member
 Join Date
 Oct 2008
 Posts
 1
 Rep Power
 0
Hello...
EN...
when I see this,,i don't understand:
Hello ceoforwww,
Our records indicate that you have never posted to our site before! Why not make your first post today by saying hello to our community in our Introductions forum.
Why not start with your first post today and become an active part of Java Forums now!
 10272008, 04:35 PM #3Member
 Join Date
 Oct 2008
 Location
 The Netherlands
 Posts
 2
 Rep Power
 0
I found the solution (on another forum):
This can easily be achieved through BigDecimal:
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class SignificantFigures {
public static void main(String[] args) {
double a = 123.4567;
double b = 0.1234567;
double c = 7.654321;
double d = 7654321;
System.out.println(getSignificant(a, 4));
System.out.println(getSignificant(b, 4));
System.out.println(getSignificant(c, 4));
System.out.println(getSignificant(d, 4));
}
public static String getSignificant(double value, int sigFigs) {
MathContext mc = new MathContext(sigFigs, RoundingMode.DOWN);
BigDecimal bigDecimal = new BigDecimal(value, mc);
return bigDecimal.toPlainString();
}
}
Similar Threads

rounding double to two decimal places
By javaMike in forum Advanced JavaReplies: 15Last Post: 03102010, 01:04 AM 
Need help rounding. =/
By yo1mcool in forum New To JavaReplies: 1Last Post: 10072008, 05:02 AM 
trying to add up random numbers into one number
By pjr5043 in forum New To JavaReplies: 4Last Post: 09152008, 02:20 PM 
Help with convert a double type number
By trill in forum New To JavaReplies: 1Last Post: 08062007, 08:48 AM 
Help with java Rounding
By silvia in forum New To JavaReplies: 1Last Post: 07202007, 07:25 AM
Bookmarks