• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a simple addition of decimal numbers

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In our project, we are taking care that all the float numbers shuld be of two decimal places only and we r not using something like BigDecimal for this to keep it simple.

So, at all the divisions I am taking care of rounding it to two decimals. Now I found a strange test case, where in adding two numbers with two decimals each resulting in more decimals as below...

S.O.P(56.56+0.56) --- gives 57.120000000000005

S.O.P(56.56f+0.56f) --- gives 57.120003

Ideally it shud just result 57.12. Can I know the reason why it is happening like above.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See Some things you should know about floating-point arithmetic.
 
rakesh verma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks marc
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you're only dealing with two digits all the time, you might want to consider keeping all of your numbers as ints. If it's possible, multiply by 100 and store as an int. Add your ints into a double and display your double divided by 100.

int i1 = 5656;
int i2 = 0056;
double d1 = i1 + i2;
System.out.println(d1/100);
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Darryl Failla:
Since you're only dealing with two digits all the time, you might want to consider keeping all of your numbers as ints. If it's possible, multiply by 100 and store as an int.



This is good advice. And if, as I suspect from the fact you're using 2 decimal places, you are doing financial calculations, it is essential that you use integer arithmetic. Floating point is unacceptably inaccurate.

Not sure about the bit suggesting displaying using a double. This could still go wrong.
[ July 19, 2006: Message edited by: Peter Chase ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic