Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (9.5k points)

Below is my code implementation to round BigDecimal values to two decimal places: 

BigDecimal rounded = value.round(new MathContext(2, RoundingMode.CEILING));

logger.trace("rounded {} to {}", value, rounded);

This is the respective output: 

rounded 0.819 to 0.82

rounded 1.092 to 1.1

rounded 1.365 to 1.4 // should be 1.37

rounded 2.730 to 2.8 // should be 2.74

rounded 0.819 to 0.82

The problem is it doesn’t round all the values. Can anyone tell me how to do this with BigDecimal or with another class/library? 

1 Answer

0 votes
by (19.7k points)

You can use RoundingMode to round the decimal value. 

my_value = my_value.setScale(2, RoundingMode.CEILING) 

If you want to learn more about Javathen go through this Java tutorial by Intellipaat for more insights.  

Related questions

0 votes
1 answer
asked Jul 9, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 19, 2019 in RPA by noah kapoor (5.3k points)
0 votes
1 answer

Browse Categories

...