• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Numeric Format

 
Makesh Kumar Ramakrishnan
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand your post.

Have you tried using java.text.DecimalFormat with a pattern like "0.##"?
 
Sanjay Singh
Ranch Hand
Posts: 37
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can try this


e.g-


Let if me know that it solve your problem or not?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc. I used DecimalFormat and It works fine. Thats what exactly wanted. Thanks for your help.


Makesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic