round double to scale - Java java.lang

Java examples for java.lang:double

Description

round double to scale

Demo Code


//package com.java2s;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] argv) throws Exception {
        double value = 2.45678;
        int scale = 2;
        System.out.println(round(value, scale));
    }//from ww  w.j  a v a2s . com

    public static double round(double value, int scale) {
        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
        double d = bd.doubleValue();
        bd = null;
        return d;
    }
}

Related Tutorials