*
The moose likes Beginning Java and the fly likes Getting rid of unwanted decimal numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting rid of unwanted decimal numbers" Watch "Getting rid of unwanted decimal numbers" New topic
Author

Getting rid of unwanted decimal numbers

Nick Hallloran
Greenhorn

Joined: Feb 09, 2010
Posts: 19
The problem asks for me to list side by side how many pounds are in a kilogram from 1 to 199. I have everything right except all these random zeros pop up after each of the pounds. For example, 3 kilograms = 6.600000000000005 pounds. I have read on this else where, and everyone suggests using the decimal format, DecimalFormat("#,###.##"); , but unfortuantely i have not been taught that yet, so i can't use it. I was thinking of using the % operator..but still cant figure out how to do it.





Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Math.round will help you


JDBCSupport - An easy to use, light-weight JDBC framework -
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

We don't know what you've been taught (or if it's a real requirement to not learn anything beyond what you're taught, which is truly frightening), so I'm not sure how we can advise you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
Do you mean the printf method with the % tags. Try %.2f. there are more details in the "formatting" link on this page in the Java™ Tutorials, and also in the Formatter class, which is more comprehensive, but also difficult to read.
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Or do a String.substring()
Christiaan Lombard
Greenhorn

Joined: Nov 22, 2009
Posts: 26
I tried this and it works:



SCJP 6
Nick Hallloran
Greenhorn

Joined: Feb 09, 2010
Posts: 19
Christiaan Lombard,

Thank you for that code...i tried it and it works..now im just trying to understand it...Can you explain how it works? Does it force it to be a double?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Nick Hallloran wrote:Can you explain how it works? Does it force it to be a double?


It's quite simple. When an operation is done, the result is of the higher range type. Hence, an int with an int is an int, a double with an int is a double, a float with an double is a double. etc. So, in this case...



The first thing that is done, is kilograms and 22, which is an int and an int, which yields an int as a result. Luckily, no precision is lost, and the int didn't overflow. The next is a cast, which results in a double. The 10 is also casted which results in a double. Finally, a division is done with two doubles which results in a double.

You can actually do this instead...



The first thing that is done, is kilograms and 22.0, which is an int and a double, which yields a double as a result. Next, a division is done with a double and an int which results in a double.

Also, since the multiply and divide has the same precedence, and goes left to right, you can also get rid of the parens...




[EDIT: After reading the questions, I think I may have implied (and answered) a different question. Sorry.]

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40


After rereading the question... here is another attempt at the answer...

Nick Hallloran wrote:
now im just trying to understand it...Can you explain how it works? Does it force it to be a double?


The main reason is that the number 22 and 10 can be represented exactly. Furthermore, the act of multiplying by 22 then dividing by 10, loses less precision than multiplying by 2.2. Quite frankly, even this is not guaranteed to work. It may break if you want to expand to a bigger range, or convert to a different unit, etc. You will need to use one of the solutions mentioned so far, if you want to guarantee it.

Henry
Nick Hallloran
Greenhorn

Joined: Feb 09, 2010
Posts: 19
OH ok i understand now....thank you very much for your help...keep watching this forum..because more than likely i may have another question pop up when i aget assigned another set of problems.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting rid of unwanted decimal numbers
 
Similar Threads
Need help with Loops
Decimals not converting to correct fractions
Metthod within method need sloving
declaring multiple variables
Speed camera craziness