Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Round to arbitrary values

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?

like image 351
kenoa Avatar asked Jan 28 '09 10:01

kenoa


People also ask

Does Java round 0.5 up or down?

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.

What is 1.5 round in Java?

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.

How do you round to 2 decimal places in Java?

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.

How do you round a value in Java?

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.


1 Answers

y = Math.round(x / 0.0025) * 0.0025
like image 134
Zach Scrivena Avatar answered Oct 06 '22 03:10

Zach Scrivena