I have a business requirement where the input values should be rounded down to the multiples provided by user. Here is the example:
Case | input | multiples | output
1 | 43.0 | 0.1 | 43.0
2 | 43.1 | 0.1 | 43.1
3 | 43.2 | 0.1 | 43.2
4 | 43.0 | 0.2 | 43.0
5 | 43.1 | 0.2 | 43.0
6 | 43.2 | 0.2 | 43.2
If the multiples
is 0.1, then the output should be in the increments of 0.1, e.g., 43.1, 43.2, etc.
If the multiples
is 0.2, then the output should be in the increments of 0.2, e.g., 43.0, 43.2, 43.4, etc.
What is the best way to do this in Java using BigDecimal
? Using BigDecimal.setScale(1, ROUND_DOWN)
I was able to restrict the number of decimal points though.