-1

In a solution to an exercise in the Book Art and Science of Java I had to write a program that converts Kilograms into the corresponding values in Pounds and Ounces. I wrote the program but when I try to convert say 1kg, the result the program gives me is: 1 kg = 2 pounds and 3.200000000000006 ounces

Now my constants are 2.2 pounds per kg and 16 ounces per pound so 3.2 ounces is correct. But not with so many 0's and that 6 at the end freaks me out.

Anyone know why this happens and how it can be solved? Thank you!

Here's the code:

/*
* File: KgsLibras.java
* Program that converts kilograms in pounds and ounces.
*/

import acm.program.*;

public class KgsLibras extends ConsoleProgram {
public void run () {
    println ("This program will convert kilograms in pounds and ounces");
    double kgs = readDouble ("Insert kgs value: ");
    double libras = kgs * LIBRAS_POR_KG;
    double oncas = (libras - (int)libras) * ONCAS_POR_LIBRA; 
    println ((int)libras + " libras" + " e " + oncas + " Onças.");
    }
private static final double LIBRAS_POR_KG = 2.2;
private static final int ONCAS_POR_LIBRA = 16;
}
  • possible duplicate of [Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?](http://stackoverflow.com/questions/177506/why-do-i-see-a-double-variable-initialized-to-some-value-like-21-4-as-21-3999996) – matsev Mar 30 '13 at 22:13

2 Answers2

2

That's just a consequence of how floating point works - literally thousands of other references to these issues here on SO alone. The short version is that not all numbers can be represented exactly using floating point numbers, which leads to oddities like the one you're seeing. This document should teach you all you should know about floating point.

In the mean time you can use format to get printf-like formatting options:

System.out.format ("%.0f libras e %.2f Onças.\n",libras,oncas);

or if you have to use that specific println method, use String's format:

println(String.format ("%.0f libras e %.2f Onças.",libras,oncas) );
Pricey
  • 518
  • 1
  • 8
  • 20
fvu
  • 32,488
  • 6
  • 61
  • 79
  • Thanks. I can´t use that solution for now since I'm learning java with the acm package (the one that the book Art and Science of Java uses) and it doesn't recognize that command. Anyway I'll read carefully the document you point out and I'm happy it's not because of some kind of error in my code but because of how floating numbers works in java. – Antonio Ricardo Diegues Silva Mar 30 '13 at 22:19
  • @AntonioRicardoDieguesSilva I'm not familiar with that acm environment, but are you sure even String can't be called as I show in my alternative example? And wrt floating point, that's an issue that affects all languages, so it's crucial that you as a programmer understand what's happening - it will save you hours of debugging and headaches later in your career :-) – fvu Mar 30 '13 at 22:24
  • No I can't use it. It gives the error: The method format(String, Object[]) in the type String is not applicable for the arguments (String, double, double) -- I guess I need to import some java class to make the method format operation since I only import acm.program package in this program. – Antonio Ricardo Diegues Silva Mar 30 '13 at 22:34
  • That's odd. What version of Java are you using? Because I had a quick look at acm and as it doesn't seem to offer facilities for formatting output you should be able to do that with standard Java API's. – fvu Mar 30 '13 at 22:37
  • Ah! For learning purposes since I'm following an old Online Stanford Programming Methodology Class I have installed the Java 6 Update 2. Maybe that is the problem. I'm also using to write code an Eclipse custom version made by Stanford University. – Antonio Ricardo Diegues Silva Mar 30 '13 at 22:41
0

You can do something like this:

String libras = String.format("$%.2f", VALUE); //For example..

Then, printing libras will suits your needs.

Regarding of your question about Why your program prints it that way, @fvu was faster than me :)

Maroun
  • 94,125
  • 30
  • 188
  • 241