Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Rounding Decimal Numbers Question

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Rounding Decimal Numbers Question

    How would I round 14.600000000001 into just 14.6? The user inputs a number(double), then the program will spit the calculation out.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Rounding Decimal Numbers Question

    Typically there two ways of producing a human friendly string from a floating point value like a double: have a look at the DecimalFormat class and the format() method of String. (PrintStream instances like System.out have a printf() method which is simlar to the latter)

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Rounding Decimal Numbers Question

    Sorry, I realise I didn't *exactly* address what you asked. Almost always it's a human friendly string that's wanted and not a - necessarily *less* accurate - floating point value. Rounding in this sense is problematic, not least because it's not at all obvious that Java floating point values are even capable of unambiguously representing the matgematical magnitude 14.6 together with it's infinity of trailling zeros.

  4. #4
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Rounding Decimal Numbers Question

    My output is 14.6000000001. How would I change it to only output 14.6?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Rounding Decimal Numbers Question

    Quote Originally Posted by Nuggets View Post
    My output is 14.6000000001. How would I change it to only output 14.6?
    See http://www.javaprogrammingforums.com...al-places.html

  6. The Following User Says Thank You to copeg For This Useful Post:

    Nuggets (March 19th, 2012)

  7. #6
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Rounding Decimal Numbers Question

    Thank You copeg!!!

Similar Threads

  1. Question about rounding
    By CoolGuy134 in forum Java Theory & Questions
    Replies: 7
    Last Post: September 27th, 2011, 04:30 PM
  2. Rounding the numbers
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 2nd, 2011, 12:45 AM
  3. Rounding long or floating numbers, or longs for that matter.
    By SPACE MONKEY in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 28th, 2011, 12:32 AM
  4. Rounding an int. Help please
    By Akim827 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2010, 12:36 AM
  5. Java Question Need Help Restarting the Values of Variables from Random Numbers So Out
    By JavaStudent1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 02:25 PM