Round Off In N Decimal Places Using Java


This simple method will round off a value in N decimal places.

1
2
3
4
5
    public static double round(double value, int decimalPlaces) {
        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP); 
        return bd.doubleValue();
    }

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

Related Posts with Thumbnails

Leave a Reply

Spam protection by WP Captcha-Free