kenoa
kenoa

Reputation: 71

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?

Upvotes: 5

Views: 3444

Answers (2)

Ray Hidayat
Ray Hidayat

Reputation: 16229

You could do this:

double step = 0.0025;
double rounded = ((int)(unrounded / step + 0.5)) * step;

Upvotes: 2

Zach Scrivena
Zach Scrivena

Reputation: 29559

y = Math.round(x / 0.0025) * 0.0025

Upvotes: 20

Related Questions