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

# Thread: Rounding Decimal Numbers Question

1. ## 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. ## 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. ## 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. ## Re: Rounding Decimal Numbers Question

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

5. ## Re: Rounding Decimal Numbers Question

Originally Posted by Nuggets
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. ## Re: Rounding Decimal Numbers Question

Originally Posted by copeg
Thank You copeg!!!