This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Java in General and the fly likes Problem with DecimalFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with DecimalFormat" Watch "Problem with DecimalFormat" New topic
Author

Problem with DecimalFormat

Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
This code (which I didn't write) occasionally prints an error like 'price: 7.949999809265137java.lang.NumberFormatException: For input string: ".795E1795E1"'

Anyone know how this could happen?

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 691
    
    1
Could you post an example that ALWAYS gets the error?
In other words, write some sample code that sets the value of price to cause the error. Then run that simple program and copy/paste all of the output here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44048
    
  33
I hope nobody is using that code in real life; it is bad enough using double values for money, but float with its lower precision is even worse.
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19728
    
  10

Besides, it is a very roundabout way of rounding a number to two decimal places.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Problem with DecimalFormat
 
It's not a secret anymore!