simple math problem
I'm doing a very simple cash register class for a school assignment and I'm having some problems with the math.
Look at the equation:
payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE + nickels * NICKEL_VALUE + pennies * PENNY_VALUE;
and
public static final double QUARTER_VALUE = 0.25;
public static final double DIME_VALUE = 0.1;
public static final double NICKEL_VALUE = 0.05;
public static final double PENNY_VALUE = 0.01;
When the equation is run the answer is technically correct however the answer(payment = 10.7399999..) ends with a repeating 9. Is this typical of java and is there some way to prevent this?
[710 byte] By [
sethbaura] at [2007-11-26 16:16:41]

> I'm doing a very simple cash register class for a
> school assignment and I'm having some problems with
> the math.
>
> Look at the equation:
>
> payment = dollars + quarters * QUARTER_VALUE +
> dimes * DIME_VALUE + nickels * NICKEL_VALUE + pennies
> * PENNY_VALUE;
>
> and
>
> public static final double QUARTER_VALUE = 0.25;
> public static final double DIME_VALUE = 0.1;
> public static final double NICKEL_VALUE = 0.05;
> public static final double PENNY_VALUE = 0.01;
>
> When the equation is run the answer is technically
> correct however the answer(payment = 10.7399999..)
> ends with a repeating 9. Is this typical of java and
> is there some way to prevent this?
2 options
1) When dealing with monetary amounts use bigDecimal.
2) Seeing as how it's a school assignment you could format the number to only go to 2 decimal places.
I am guessing the second one would be your best bet
if you aren't allowed to use a formatter you could also think about multiplying by adding .005 multiplying by 100 truncate the number and then take that number and divide by 100.
(these are off the top of my head and the last suggestion was off the cuff, don't think it will work with all cases, more of something you can evaluate and think about)
You could of course talk to your teacher and see how he was expecting you to deal with it, rather hard to give a suggestion without knowing what level the course is...
This is not Java-specific. All languages that use the same IEEE floating point format (-754?) will have this.
Short version: Some numbers that can be stored exactly in base-10 cannot be stored exactly in base-2. For instance, 1/10 cannot be represented exactly in binary, just as 1/3 cannot be in decimal. Both are repeating fractions.
See the following for more details.
[url=http://java.sun.com/developer/JDCTechTips/2003/tt0204.html#2]SOME THINGS YOU SHOULD KNOW ABOUT FLOATING-POINT ARITHMETIC[/url]
[url=http://docs.sun.com/source/806-3568/ncg_goldberg.html]What Every Computer Scientist Should Know About Floating-Point Arithmetic[/url]
Another good (slightly simpler) FP explanation:
http://mindprod.com/jgloss/floatingpoint.html
You can use java.text.DecimalFormat to format the numbers properly for display. This will work well enough for you, but in real financial applications, float and double would not be used for monetary calculations.
Why not work in cents instead of dollars? Then you can work with int instead of double.
> Why not work in cents instead of dollars? Then you
> can work with int instead of double.
True it will work with the case he has listed, but better off with
1) him learning about the reason that you shouldn't use these values for anything financial
2) Saving him time when the next assignment builds on his last one.
(Everything i've been taught about coding was to code so that it was as flexible as possible).
>One solution: Don't do calcuation in one long equation. Break into smaller calculation. See it will solve the problem.
>Second solution: use DecimalFormat from java. You can do
NumberFormat f = NumberFormat.getInstance(payment);
if (f instanceof DecimalFormat) {
((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true);
}
>Further reading in this link: http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html