Forums Register Login

rounding off in java

+Pie Number of slices to send: Send
Hi

How do I round off the decimal in java to the nearest .05
e.g:
1.499 should 1.5.
But 1.49 should still be 1.49.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
Hi Mani

Thanks a lot. I am not too clear how to achieve what I want. DecimalFormat is more for formatting the decimal numbers.
I am interested in rounding off.
Can you please give an example using the API
+Pie Number of slices to send: Send
There is a method called setMaximumFractionDigits() in that class.
That might be useful.
+Pie Number of slices to send: Send
 

Originally posted by Vasim Patel:
How do I round off the decimal in java to the nearest .05

Since 0.05 is a decimal number, and float/double are binary approximations of decimal numbers, you can't be guaranteed to round exactly using them. If you just want to get close, try this:In words, you shift the rounding scale to be 1 instead of 0.05, round the value to nearest integer, and shift back. If you need exact decimal values, use java.math.BigDecimal.
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3314 times.
Similar Threads
diff b/w static final & final static
problem with JTable printing (urgent)
code doubt
what is the latest version of java.how is it?
Problem with packages!!!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:12:47.