Two Laptop Bag*
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
Author

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).

Thanks,


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

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
sscce.org
Sanjay Singh
Ranch Hand

Joined: Mar 02, 2006
Posts: 37

I think you can try this


e.g-


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


Regards, Sanjay Singh
SCJP-1.6, OCEWCD 6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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.


Makesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Numeric Format