aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 13884
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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: 36599
    
  16
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: 18141
    
  39


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: 36599
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange Conversion
 
Similar Threads
why -0.0 == 0.0 true ??? and
Using JOptionPane in NoSalesException
incompatible types: found double required float error;
Division by zero double
problem with printing double value as 0.00 ?