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.
maintaining 2 decimals for double when the decimals are 00
venkata vinay bysani
Joined: Feb 09, 2010
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)
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.