Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange Conversion

 
Naveen puttu
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15440
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
This is the piece of code inside the main method

 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21409
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yeah

So basically what im trying to achieve is impossible
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic