Results 1 to 3 of 3
  1. #1
    rikribbers is offline Member
    Join Date
    Oct 2008
    The Netherlands
    Rep Power

    Default 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.

    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;

    // 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.


  2. #2
    ceoforwww is offline Member
    Join Date
    Oct 2008
    Rep Power

    Default Hello...


    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!

  3. #3
    rikribbers is offline Member
    Join Date
    Oct 2008
    The Netherlands
    Rep Power


    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

  1. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 01:04 AM
  2. Need help rounding. =/
    By yo1mcool in forum New To Java
    Replies: 1
    Last Post: 10-07-2008, 06:02 AM
  3. trying to add up random numbers into one number
    By pjr5043 in forum New To Java
    Replies: 4
    Last Post: 09-15-2008, 03:20 PM
  4. Help with convert a double type number
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:48 AM
  5. Help with java Rounding
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 08:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts