• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Strange Conversion

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi

I want to display the double 0.0 as double 0.00

I tried decimalformat . but i guess the static toString method of double is rendering the value as 0.0

How would i get 0.00 as a double number

Cheers
Max
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

What exactly did you try - please post your source code. Please post your code inside code tags.
 
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might find something useful in the Java™ Tutorials and the Formatter class.
 
Naveen puttu
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the piece of code inside the main method

 
Campbell Ritchie
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's wrong with the parsing method of the Double class?

Remember a double does not have any formatting, only a value.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And this....



doesn't do anything useful. All it does is formats the double -- which results in a formatted string. And then parses it back to a double, which doesn't have any formatting.

As already mentioned, doubles do *not* have any formatting. If you want formatting, you will have to format it to a string, which you have done, and then use the string instead.

Henry
 
Naveen puttu
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah

So basically what im trying to achieve is impossible
 
Campbell Ritchie
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naveen Bangalore wrote:Yeah

So basically what im trying to achieve is impossible

No, it is by no means impossible. I have given links to one way to do it. What you have to do is remember there is a difference between how a number is stored as a primitive (you can find the details in this Wikipedia article and its links to IEEE754-1985, but it is by no means easy to understand), and how a number is displayed on screen. You should be able to achieve displays with DecimalFormat or the easier methods in the links I gave. You may be using DecimalFormat incorrectly.

You might be able to do it differently with this class, but values not ending with .0 .5 .25 .125 etc should be entered as "1.23" not 1.23 to maintain precision.
 
It runs on an internal combustion engine. This ad does not:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic