• 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

Calculation Problem...

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have problem with calculation of these numbers.
When I muliply 90000*.35 I get a value with decimals not the expected result, but the same if I do with 90000*.45 I get the correct output. Then again I have done similar calculation with 90000*.55 again I got wrong value(not the expected). We have tried one more combination of the decimal mulitplication .35 with 70000 which it gave the right result.

Can I know what could be the reason for this mismatch.

Code which we have executed is given below.

System.out.println((90000*.35));//31499.999999999996
System.out.println((90000*.45)); //40500.0
System.out.println((90000*.55)); //49500.00000000001
System.out.println((70000*.35));//24500.0

Thanks & Regards,

Joseph
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll always get precision problems with double.
Did you try using BigDecimal ?
 
Joseph Samuel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not yet tried using BigDecimal. Now the code which we have already been deployed in Production is using with double. We used double because it gives higher precission. But here it is creating problem. Problems like coming up less than 1 or more than 1 errors are coming.

BigDecimal will it solve this problem. BigDecimal can it be used for Arithmatic operation without any problem?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can set BigDecimal's rounding mode and scale. See the API for more.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic