File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes maintaining 2 decimals for double when the decimals are 00 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "maintaining 2 decimals for double when the decimals are 00" Watch "maintaining 2 decimals for double when the decimals are 00" New topic
Author

maintaining 2 decimals for double when the decimals are 00

venkata vinay bysani
Greenhorn

Joined: Feb 09, 2010
Posts: 25
Hi All,

I need to send the double value or Double object to another application with 2 decimals, i am facing problem if the decimals are having 0's if the value is 2.54 its going fine, if its 2.50 its going as 2.5 (where it has to go as 2.50) similar case when it is 2.00 its going as 2.0 (it has to go as 2.00)

i am converting a string to double as


by this conversion its not giving the last 0. Is there any way to retain that last 0 as well if i want a 2 decimal point double value.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

A double with a value of 2.5 is the exact same double as one with a value of 2.50. The number of decimal places only comes into play in the String when you format the double. If your requirements really dictate a certain number of decimal places in the stored numerical value, you'll have to use BigDecimal. However, unless you're dealing with money or with numbers outside of double's range or precision, most likely you can just sling doubles around and only worry about the precision when it comes time to display.

Also, why are you mixing floats and doubles? There's almost never a reason to use float in Java, except maybe in a small-memory or small-cpu context. And if you are using them, it doesn't make sense to mix them with double.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: maintaining 2 decimals for double when the decimals are 00
 
Similar Threads
Problems w/ formatted text
DecimalFormat dropping zeros
formatting amount
How to convert String to float with exact decimal value
How to get two digit decimal point in double