• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

maintaining 2 decimals for double when the decimals are 00

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic