In Java, how do I round to an arbitrary value? Specifically, I want to round to .0025 steps, that is:
0.032611 -> 0.0325
0.034143 -> 0.0350
0.035233 -> 0.0350
0.037777 -> 0.0375
...
Any ideas or libs?
In mathematics, if the fractional part of the argument is greater than 0.5, it is rounded to the next highest integer. If it is less than 0.5, the argument is rounded to the next lowest integer.
Today, I saw this behaviour of Java and Javascript's Math. round function. It makes 1.40 to 1 as well as -1.40 to -1. It makes 1.60 to 2 as well as -1.60 to -2. Now, it makes 1.5 to 2.
1. DecimalFormat(“0.00”) We can use DecimalFormat("0.00") to ensure the number always round to 2 decimal places. For DecimalFormat , the default rounding mode is RoundingMode.
Java Math round()The round() method rounds the specified value to the closest int or long value and returns it. That is, 3.87 is rounded to 4 and 3.24 is rounded to 3.
y = Math.round(x / 0.0025) * 0.0025
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With