Results 1 to 16 of 16
  1. #1
    javaMike is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Question rounding double to two decimal places

    is there an easy way to round a double to two decimal places?

    i.e. 1.98999 would round to 1.99

    the math.round method only rounds to nearest integer.

    decimalformat class converts number to a string.

    i figure there must be an easy way to do this...?? pls advise

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    DecimalFormat really is the easiest option to use:
    Java Code:
    double roundTwoDecimals(double d) {
            	DecimalFormat twoDForm = new DecimalFormat("#.##");
    		return Double.valueOf(twoDForm.format(d));
    }

  3. #3
    shalomshachne is offline Member
    Join Date
    Aug 2008
    Posts
    1
    Rep Power
    0

    Default Thanks for help with that...

    Thanks for your help with that post. Am I insane, or does anyone else think that it's crazy not to have a simple already built function in Java to round a double to specified # of decimal places?

    Am I missing something? Why would everyone want to custom code this function?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,275
    Rep Power
    25

    Default

    Another way to change the value of the double, you'd need to do something like this:
    double dbl = 12.3456;
    int ix = (int)(dbl * 100.0); // scale it
    double dbl2 = ((double)ix)/100.0;
    System.out.println("dbl=" + dbl + ", dbl2=" + dbl2); //dbl=12.3456, dbl2=12.34

    To round, you'd need to add in some value.
    Last edited by Norm; 08-20-2008 at 06:41 PM.

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    You are not, by chance, violating the number one rule, are you:

    Never use float or double for money. Its a sin.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Never use float or double for money. Its a sin.
    I can't disagree with you as you know a heck of a lot more Java than I'll ever know, but do you think this rule needs to be rigidly enforced for folks taking intro to Java courses?

  7. #7
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    I think teaching the use of float/double for currency is a mortal sin.

    Sadly, some teachers don't know any better.

    On this, and other forums, you see folks just starting out, clearly doing homework, and they all use floats or doubles.

    The real problem is that it works for wimpy testing, and when you put it in production, stuff doesn't add up, the accountants get very mad, and you have a real mess to fix.

    Cobol had fixed point built in from day one. For a reason.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, can't argue with any of that. As you state though, most teachers don't know any better, and most students here are coding with constraints set by these same teachers.

    Again, thanks for posting your thoughts on this.

  9. #9
    netstat1 is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default

    So what do you suggest using? BigDecimal?

    Quote Originally Posted by fishtoprecords View Post
    You are not, by chance, violating the number one rule, are you:

    Never use float or double for money. Its a sin.

  10. #10
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by netstat1 View Post
    So what do you suggest using? BigDecimal?
    You can if you must, but for most folks, you can simply store money as "pennies" rather that "dollars and cents" and use integer.

    Since an integer can hold +-2billion, that is +- 2 billion cents, or 20 million dollars. For most applications that is enough.

  11. #11
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Fish is right when you do have to deal with fractional amounts.

    BigDecimal is specifically designed to handle very large values, exact values, calculations with a bunch of specific rounding methods, etc.

  12. #12
    Joao is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default

    What about:

    public static double roundToDecimals(double d, int c) {
    int temp=(int)((d*Math.pow(10,c)));
    return (((double)temp)/Math.pow(10,c));
    }

    It gives you control of how many numbers after the point are needed. d=number to round; c = number of decimal places.

  13. #13
    Spaznur is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default

    public static double round2(double num) {
    double result = value * 100;
    result = Math.round(result);
    result = result / 100;
    return result;
    }



    This works fine.

  14. #14
    Menre is offline Member
    Join Date
    Feb 2008
    Posts
    78
    Rep Power
    0

    Default

    Maybe nice to blame teachers and students on this issue. But I aggree with these questions.

    ...not to have a simple already built function in Java to round a double to specified # of decimal places?
    ... Why would everyone want to custom code this function?

  15. #15
    vanstacks is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default

    If my code have many variables that need to have 2 decimal place format, then I have to use the Math.round function for every single one of them?

    Or, there's any other way?

    Thank you

  16. #16
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

Similar Threads

  1. round to two decimal places
    By javaMike in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 02:01 AM
  2. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM
  3. Replies: 7
    Last Post: 01-12-2008, 09:02 PM
  4. Capping decimal places
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-20-2007, 09:28 PM
  5. Help with java Rounding
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 07: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
  •