# Thread: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - why?

1. Member Join Date
Aug 2012
Posts
6
Rep Power
0

## double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - why?

public class Lincoln7
{
public static void main (String[] args)
{
int units;
double money;

money = 16.65;
units = (int)(money * 100.0);
System.out.println ("wrong answer " + units );

}}  Reply With Quote

2. ## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

Moving to the new to Java section as this shouldn't be posted in a non-question section of the forums.

Google and read up on the problem with using digital hardware to try to represent floating point numbers. The issue is that except for some defined numbers, floating point numbers are not represented fully accurately but rather only to a good approximation. So 16.65 will not be exact but will be somewhere close to this value, perhaps a little above or a little below. next, understand that when you cast a double to an int, you don't round but rather you truncate the number, so that 1664.9999999999 will become 1664.  Reply With Quote

3. Member Join Date
Aug 2012
Posts
6
Rep Power
0

## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

I hate to be obtuse but - - ‘Not represented accurately’ I don’t understand.
- A little bit above or below --- why?
-  Reply With Quote

4. ## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

try

System.out.println(1.03 - .42);

According to "Effective Java" from J. Bloch the reason is "The float and double types are particularly illsuited for monetary calculations because it is impossible to represent 0.1 (or any
other negative power of ten) as a float or double exactly.".

The alternatives are "to keep track of the decimal point yourself" or to use the class BigDecimal and its relatated methods.
Last edited by j2me64; 08-30-2012 at 09:25 PM.  Reply With Quote

5. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - Originally Posted by jayjay5 I hate to be obtuse but - - ‘Not represented accurately’ I don’t understand.
- A little bit above or below --- why?
-
Try and represent 1/3 as a decimal and then get back to me.  Reply With Quote

6. Member Join Date
Aug 2012
Posts
6
Rep Power
0

## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

I am not referring to a recurring decimal. The numbers is 16.65; that is to say 16 dollars and 65 cents. I want to convert this to pennies. Multiply by 100 and cast to an int. JAVA has an obvious malfunction.  Reply With Quote

7. Moderator  Join Date
Jul 2010
Location
California
Posts
1,638
Rep Power
12

## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -  Reply With Quote

8. ## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - Originally Posted by jayjay5 I am not referring to a recurring decimal. The numbers is 16.65; that is to say 16 dollars and 65 cents. I want to convert this to pennies. Multiply by 100 and cast to an int. JAVA has an obvious malfunction.
Regarding "Java has an obvious malfunction", um, no, the malfunction is still in your understanding, and you still need to understand the problems with representing floating point numbers on a digital computer. This is a problem shared by all programming languages, because it has nothing to do with the language itself but rather is a limitation in the hardware. Please read the links given to you in your cross-posts. And don't feel bad or put down as this is a concept that all of us must struggle to fully understand at one time or another, and this is just your time.  Reply With Quote

9. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - Originally Posted by jayjay5 I am not referring to a recurring decimal. The numbers is 16.65; that is to say 16 dollars and 65 cents. I want to convert this to pennies. Multiply by 100 and cast to an int. JAVA has an obvious malfunction.
I don't think you understood me.
Can you exactly represent 1/3 as a decimal?

The answer to that is exactly the same as the answer to why you can't represent 16.65 as a binary. Which applies to all floating point numbers in any language.

Of course, failing that, there's always Goldberg.  Reply With Quote

10. ## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

From the book "Effective Java", Second Edition, J. Bloch:

Item 48: Avoid float and double if exact answers are required
Study the class BigDecimal!  Reply With Quote

11. Member Join Date
Aug 2012
Posts
6
Rep Power
0

## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

I hava a pocket calculator the has the amazing ability to multiply 16.65 by 100 and produce the correct answer.  Reply With Quote

12. ## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - Originally Posted by jayjay5 I hava a pocket calculator the has the amazing ability to multiply 16.65 by 100 and produce the correct answer.
Congratulations. Do you have any more worthless gems to share?

db  Reply With Quote

13. ## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - Originally Posted by jayjay5 I hava a pocket calculator the has the amazing ability to multiply 16.65 by 100 and produce the correct answer.
All this proves is that you've read none of the links provided here and in your cross-posts. You might consider reading or re-reading them before demonstrating any more ignorance.  Reply With Quote

14. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - Originally Posted by jayjay5 I hava a pocket calculator the has the amazing ability to multiply 16.65 by 100 and produce the correct answer.
Calculator hardware != computer hardware.
See here.

Though I suspect you won't bother reading that one either...  Reply With Quote

15. Member Join Date
Aug 2012
Posts
6
Rep Power
0

## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

The solution is rather simple: consider (1/3)*3; this would produce .999999…. ; if you add/subtract fuzz; .0000000001 (the smallest binary number possible) ; Floating Point : Java Glossary ; If the binary representation changes dramatically; accept the number fuzzed.  Reply With Quote

16. ## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

A lot of calculators use BCD math (Binary Coded Decimal) so they can indeed perform the calculation 16.65*100 == 1665 exactly; they still have problems representing 1/3 in an exact decimal expansion though (it can't be done). The IEEE754 number system is like a finite bunch of cards, marked with the fractions 1/2, 1/4, 1/8, 1/16 etc. one card for each fraction. Given a number you can pick any cards you like as long as the sum of those fractions comes as close to that target number as possible. For the number 1/10 you can't find a combination of cards that total 1/10 exactly.

kind regards,

Jos  Reply With Quote

17. Member Join Date
Aug 2012
Posts
6
Rep Power
0

## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 -

It is not a question of understanding - it is a question of acceptance. I have written graphics programs (non JAVA) in which graphic images contract and expand numerous times (it’s a matrix algebra thing). Yes, in this case there must be a lower limit to which an image can be reduced and latter accurately expanded. The consequence is accuracy drift over thousands of iterations. Yes – I do understand binary repeating numbers. What about fuzz – the concept has been around for a long time. Do I accept that JAVA cannot convert 16 dollars and 65 cents to pennies?  Reply With Quote

18. ## Re: double to int conversion -- 3 simple lines of code -- prints wrong answer 1664 - Originally Posted by jayjay5 It is not a question of understanding - it is a question of acceptance. I have written graphics programs (non JAVA) in which graphic images contract and expand numerous times (it’s a matrix algebra thing). Yes, in this case there must be a lower limit to which an image can be reduced and latter accurately expanded. The consequence is accuracy drift over thousands of iterations. Yes – I do understand binary repeating numbers. What about fuzz – the concept has been around for a long time. Do I accept that JAVA cannot convert 16 dollars and 65 cents to pennies?
No, you would never use floating point variables to represent financial concepts in *any* programming language. This is what Java has the BigDecimal type. There is extra overhead required for using BigDecimal in terms of memory and speed, but for critical financial calculations, it is worth this price.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•