Here you can find the source of roundDouble(double value, int afterDecimalPoint)
Parameter | Description |
---|---|
value | the double value |
afterDecimalPoint | the number of digits after the decimal point |
public static double roundDouble(double value, int afterDecimalPoint)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from ww w .ja v a 2 s .c o m*/ * Rounds a double to the given number of decimal places. * * @param value the double value * @param afterDecimalPoint the number of digits after the decimal point * @return the double rounded to the given precision */ public static /*@pure@*/ double roundDouble(double value, int afterDecimalPoint) { double mask = Math.pow(10.0, (double) afterDecimalPoint); return (double) (Math.round(value * mask)) / mask; } /** * Rounds a double to the next nearest integer value. The JDK version * of it doesn't work properly. * * @param value the double value * @return the resulting integer value */ public static /*@pure@*/ int round(double value) { int roundedValue = value > 0 ? (int) (value + 0.5) : -(int) (Math.abs(value) + 0.5); return roundedValue; } }