How would I round 14.600000000001 into just 14.6? The user inputs a number(double), then the program will spit the calculation out.
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.
How would I round 14.600000000001 into just 14.6? The user inputs a number(double), then the program will spit the calculation out.
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)
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.
My output is 14.6000000001. How would I change it to only output 14.6?
Nuggets (March 19th, 2012)