GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Strange Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strange Conversion" Watch "Strange Conversion" New topic
Author

Strange Conversion

Naveen puttu
Ranch Hand

Joined: Sep 15, 2009
Posts: 88

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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Welcome to JavaRanch.

What exactly did you try - please post your source code. Please post your code inside code tags.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39072
    
  23
You might find something useful in the Java™ Tutorials and the Formatter class.
Naveen puttu
Ranch Hand

Joined: Sep 15, 2009
Posts: 88
This is the piece of code inside the main method

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39072
    
  23
What's wrong with the parsing method of the Double class?

Remember a double does not have any formatting, only a value.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18875
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Naveen puttu
Ranch Hand

Joined: Sep 15, 2009
Posts: 88
Yeah

So basically what im trying to achieve is impossible
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39072
    
  23
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.
 
GeeCON Prague 2014
 
subject: Strange Conversion