Home arrow static arrow Java Programming [Archive] - java and Numbers precission
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - java and Numbers precission
This topic has 7 replies on 1 page.

Posts:8
Registered: 12/2/03
java and Numbers precission  
Jun 15, 2004 2:59 AM



 
Hi all.
I've found java does not treat numbers as I expected. For instance, if I write the code:
public class multi {    public static void main(String[] args) {        System.out.println(0.29*100.0);    }}

I'm very surprised to see that the console shows me the result: 28.999999999999996 (obviously it should be 29.0). The only way I've found to get the correct result, it has been casting the numbers by attaching 'f' at the end of each other (so casting them explicitly to floats).

Has anyone found the same problem, is there an easy way to solve it without doing the explicit cast?

Thanks all.
 

Posts:13,252
Registered: 24/10/97
Re: java and Numbers precission  
Jun 15, 2004 3:22 AM (reply 1 of 7)



 
http://docs.sun.com/source/806-3568/ncg_goldberg.html

To display decimals use class DecimalFormat.
 

Posts:3,258
Registered: 00-08-28
Re: java and Numbers precission  
Jun 15, 2004 4:39 PM (reply 2 of 7)



 
http://docs.sun.com/source/806-3568/ncg_goldberg.html

To display decimals use class DecimalFormat.

this site has been flowting around for a few weeks now and no doubt is a wonderful link to develop understanding of the subject
 

Posts:24,036
Registered: 2/3/03
Re: java and Numbers precission  
Jun 15, 2004 4:47 PM (reply 3 of 7)



 
this site has been flowting around for a few weeks now
and no doubt is a wonderful link to develop
understanding of the subject

A few weeks? LOL.
 

Posts:24,036
Registered: 2/3/03
Re: java and Numbers precission  
Jun 15, 2004 4:50 PM (reply 4 of 7)



 
Hi all.
I've found java does not treat numbers as I expected.

You'll find that to be the case with any programming language, nay, computer. Binary is incapable of perfectly representing certain numbers. But then, so is decimal...

As was mentioned, use DecimalFormat to display your numbers in the format of your choosing.
 

Posts:447
Registered: 3/8/01
Re: java and Numbers precission  
Jun 15, 2004 6:56 PM (reply 5 of 7)



 
The only difference between Java and other programming languages is that Java doesn't bother to make numbers look human-readable (although it does try to print as few decimal places as necessary, and does that sometimes). In C++ for example, using printf, a maximum of 6 decimal places are printed by default (unless you specify more). Try doing something like printf(".12lf", x), for 12 decimal places.
 

Posts:3,258
Registered: 00-08-28
Re: java and Numbers precission  
Jun 16, 2004 6:33 AM (reply 6 of 7)



 
this site has been flowting around for a few weeks
now
and no doubt is a wonderful link to develop
understanding of the subject

A few weeks? LOL.

I meant @ these forums.
 

Posts:24,036
Registered: 2/3/03
Re: java and Numbers precission  
Jun 16, 2004 6:57 AM (reply 7 of 7)



 
A few weeks? LOL.

I meant @ these forums.

I know. That's what [url=http://onesearch.sun.com/search/developers/index.jsp?and=&nh=100&phr=what+every+computer+scientist+should+know&qt=&not=&field=&since=&col=devforums&rf=1&Search.x=30&Search.y=12]made me giggle[/url]. It's been posted on the forum for at least the past three years or so...
 
This topic has 7 replies on 1 page.