File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Numeric Format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Numeric Format" Watch "Numeric Format" New topic

Numeric Format

Makesh Kumar Ramakrishnan
Ranch Hand

Joined: Jan 07, 2006
Posts: 88
When I use Double.toString(), it converts the value 24 to 24.0. I want to display the value as it is(ie.. only 24).. But when I have a number like 24.5, then it should be displayed as 24.5
How can I do this functionality by using sigle function?
That means when I pass 24/24.5(Numeric value) to a function, then it should return 24/24.5(String). It shouldn't return 24.0/24.5(String).


Makesh<br /> <br />SCJP | SCWCD | SCBCD | SCEA
marc weber

Joined: Aug 31, 2004
Posts: 11343

I'm not sure I understand your post.

Have you tried using java.text.DecimalFormat with a pattern like "0.##"?

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Sanjay Singh
Ranch Hand

Joined: Mar 02, 2006
Posts: 37

I think you can try this


Let if me know that it solve your problem or not?

Regards, Sanjay Singh
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
You could try altering your formatting String
Work backwards from
Now, try and alter the string you use for your formatting.
Now, try and work back to the bit you have afterwards, the "%.1" bit. You can alter that depending on whether number % 1 == 0 or not.

This won't work on Java 1.4.2 or older.
Makesh Kumar Ramakrishnan
Ranch Hand

Joined: Jan 07, 2006
Posts: 88
Thanks Marc. I used DecimalFormat and It works fine. Thats what exactly wanted. Thanks for your help.

I agree. Here's the link:
subject: Numeric Format
It's not a secret anymore!