rounds double value upper half - Java java.lang

Java examples for java.lang:Math Operation

Description

rounds double value upper half

Demo Code


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

public class Main {
    /**/*from  ww w  . j a v  a  2 s .co m*/
     * rounds upper half
     * 
     * @param val
     * @param decPlaces
     * @return
     */
    public static double roundHalfUp(double val, int decPlaces) {
        BigDecimal bd = new BigDecimal(val);
        bd = bd.setScale(decPlaces, BigDecimal.ROUND_HALF_UP);
        val = bd.doubleValue();
        return val;
    }

    /**
     * rounds upper half
     * 
     * @param val
     * @return
     */
    public static double roundHalfUp(double val) {
        BigDecimal bd = new BigDecimal(val);
        bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
        val = bd.doubleValue();
        return val;
    }
}

Related Tutorials