This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
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
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply 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: 17
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: 5783
    
    5

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
formatting amount
How to get two digit decimal point in double
DecimalFormat dropping zeros
How to convert String to float with exact decimal value
Problems w/ formatted text