Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

maintaining 2 decimals for double when the decimals are 00

 
venkata vinay bysani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic