Rounds a given double value to 4 digits behind the comma. - Java java.lang

Java examples for java.lang:Math Value

Description

Rounds a given double value to 4 digits behind the comma.

Demo Code

/***************************************************************************
 * Copyright (c) 2016 the WESSBAS project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.//from  www.  ja v a2s .  c o m
 ***************************************************************************/
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedList;
import java.util.List;

public class Main{
    /**
     * Rounds a given <code>double</code> value to 4 digits behind the comma.
     *
     * @param value  value to be rounded.
     *
     * @return  the rounded value.
     */
    public static double round(final double value) {

        return ((double) Math.round(value * 10000 + 0.00005)) / 10000;
    }
    /**
     * Rounds a given <code>BigDecimal</code> value to 4 digits behind the
     * comma.
     *
     * @param value  value to be rounded.
     *
     * @return  the rounded value.
     */
    public static BigDecimal round(final BigDecimal value) {

        return new BigDecimal(MathUtil.round(value.doubleValue()));
    }
}

Related Tutorials