aspose file tools*
The moose likes Java in General and the fly likes DecimalFormat drops the 0s after decimal point. I need to keep them. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "DecimalFormat drops the 0s after decimal point. I need to keep them." Watch "DecimalFormat drops the 0s after decimal point. I need to keep them." New topic
Author

DecimalFormat drops the 0s after decimal point. I need to keep them.

Sri Kor
Ranch Hand

Joined: Sep 03, 2001
Posts: 35
Hi,

I have to show the number as is without dropping the end 0s after decimal.

e.g. I need to show 12 as 12, 13.0 as 13.0 14.50 as 14.50. But the decimal format removes the last zero.

Unfortunately, JFreeChart tooltip logic requires a decimal format to be specified to do anything like this.

Any idea how we can achieve this.

thanks in advance.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

What do you mean by "I need to keep them"? Are you using DecimalFormat to parse a double from a string? And is now wondering why when you print the double, the format of the double is gone?

If you are... A double is a value. It contains no formatting what-so-ever. So, there is no such a thing as "keep them" -- any formatting is lost when the string is converted to a double.

Henry


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

Joined: Mar 05, 2008
Posts: 3011
    
  10
How are you specifying the DecimalFormat now? Have you looked at the API for DecimalFormat? If it's something like

try instead
Sri Kor
Ranch Hand

Joined: Sep 03, 2001
Posts: 35
Mike Simmons wrote:How are you specifying the DecimalFormat now? Have you looked at the API for DecimalFormat? If it's something like

try instead


I can't use formatting like this. Because, if I have a number like 29 I have to show it as 29 in the JFreeChart. If I use a format like the above, it will change the number 29 to 29.00.
Sri Kor
Ranch Hand

Joined: Sep 03, 2001
Posts: 35
Henry Wong wrote:What do you mean by "I need to keep them"? Are you using DecimalFormat to parse a double from a string? And is now wondering why when you print the double, the format of the double is gone?

If you are... A double is a value. It contains no formatting what-so-ever. So, there is no such a thing as "keep them" -- any formatting is lost when the string is converted to a double.

Henry


By default a number like 12.20 is shown as 12.2. But, I need to show it as 12.20. If the number is 12.220, I need to show 12.220. But, JFreeChart which uses decimal format drops 0 at the end.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

You could consider using BigDecimal. If you create that using "12.220" it will keep being 12.220, including when calling it's toString method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DecimalFormat drops the 0s after decimal point. I need to keep them.