CookingJava
CookingJava

Reputation: 63

Round off up to two decimal places

I want to round of up to two decimal places. That round off should happen to the very basic level which we have learned in school.

new BigDecimal(inputValue).setScale(2, What should I use here?);

I've tried mostly all available parameters but all works partially.

Can't share example because there could be so many, but just want my value to be round off up to two decimal place at very basic level.

I'm little tired of searching the exact parameter to accomplish this task.

Note: Basic level means not using ROUND_HALF_EVEN etc.

Upvotes: 3

Views: 129

Answers (2)

dna8675
dna8675

Reputation: 677

You should try this:

new BigDecimal(inputValue).setScale(2, RoundingMode.HALF_UP)

This way values 0.005 => 0.01, and values 0.004 => 0.0.

Also, see this table for the behavior of other RoundingMode options:

+--------------+----+------+---------+-------+---------+-----------+-----------+---------------------------+
| Input Number | UP | DOWN | CEILING | FLOOR | HALF_UP | HALF_DOWN | HALF_EVEN | UNNECESSARY               |
+--------------+----+------+---------+-------+---------+-----------+-----------+---------------------------+
| 5.5          | 6  | 5    | 6       | 5     | 6       | 5         | 6         | throwĘArithmeticException |
+--------------+----+------+---------+-------+---------+-----------+-----------+---------------------------+
| 2.5          | 3  | 2    | 3       | 2     | 3       | 2         | 2         | throwĘArithmeticException |
+--------------+----+------+---------+-------+---------+-----------+-----------+---------------------------+
| 1.6          | 2  | 1    | 2       | 1     | 2       | 2         | 2         | throwĘArithmeticException |
+--------------+----+------+---------+-------+---------+-----------+-----------+---------------------------+
| 1.1          | 2  | 1    | 2       | 1     | 1       | 1         | 1         | throwĘArithmeticException |
+--------------+----+------+---------+-------+---------+-----------+-----------+---------------------------+
| 1            | 1  | 1    | 1       | 1     | 1       | 1         | 1         | 1                         |
+--------------+----+------+---------+-------+---------+-----------+-----------+---------------------------+
| -1           | -1 | -1   | -1      | -1    | -1      | -1        | -1        | -1                        |
+--------------+----+------+---------+-------+---------+-----------+-----------+---------------------------+
| -1.1         | -2 | -1   | -1      | -2    | -1      | -1        | -1        | throwĘArithmeticException |
+--------------+----+------+---------+-------+---------+-----------+-----------+---------------------------+
| -1.6         | -2 | -1   | -1      | -2    | -2      | -2        | -2        | throwĘArithmeticException |
+--------------+----+------+---------+-------+---------+-----------+-----------+---------------------------+
| -2.5         | -3 | -2   | -2      | -3    | -3      | -2        | -2        | throwĘArithmeticException |
+--------------+----+------+---------+-------+---------+-----------+-----------+---------------------------+
| -5.5         | -6 | -5   | -5      | -6    | -6      | -5        | -6        | throwĘArithmeticException |
+--------------+----+------+---------+-------+---------+-----------+-----------+---------------------------+

Please refer to the documentation for a full explanation. Source:Java Doc

Upvotes: 1

Gaurav Mall
Gaurav Mall

Reputation: 2412

What you need to do is this:

new BigDecimal(inputValue).setScale(2, RoundingMode.UP)

The RoundingMode.UP is just many of the options you can use in the RoundingMode class.

There are many more. For instance, here we use the UP which rounds everything up. You can see all the values here: https://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html

Edit: I understand what you want to do. You want 45.423 to become 45.42 while 45.55 to become 45.56. But in Java, anything with a .5 in the end would round down or as it's known to floor it. You can't round 45.555 to 45.56 without using UP, but that also means that 45.243 would become 45.25. So there is a tradeoff. No one solution for all.

Upvotes: 1

Related Questions