• Post Reply Bookmark Topic Watch Topic
  • New Topic

ROUND_HALF_UP with DecimalFormat  RSS feed

 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
can we do ROUND_HALF_UP with DecimalFormat.My problem is that when i format a number like 1257.125 with ###,##.00 i am getting 1257.12 but i need 1257.13.
Can some body help me.
THanks
kundan
 
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DecimalFormat is a class in the java.text package - you can't do any mathmatical operations with a DecimalFormat. If you need to round things, you typically would use the methods supplied by the Math class.
 
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add 0.005 before formating
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI stefan
I can not add .005 because decimal places are coming from userpreference. IT may happen that for some users i have to add .00005
if the format structure will be ##,####.0000 so it varies.
Can you or any body else provide some better solutions.I thank you for your this solution because it has definitely shown me some way to proceed.
I am still looking for a better solution
Thanks
kundan
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all
I have solved my problems .I got the idea from stefan's reply.
Now what i am doing is
1) tokenizing pattern ####,##.00
2)taking no of digits after decimal
3) creating that no of 0 and then appending 1 into it like .001 for .00 or .0001 for .000
4) Adding that to my original no then doing decimalFormat.format
THanks stefan
and all the javaranch members

kundan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!