round a double value by decimal places - Android java.lang

Android examples for java.lang:Double

Description

round a double value by decimal places

Demo Code


//package com.java2s;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static double round(double value, int places) {
        if (places < 0)
            throw new IllegalArgumentException();

        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(places, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }/*from w  w  w  .  ja v  a 2  s.  co  m*/
}

Related Tutorials