Here you can find the source of roundDecimals(double x, int decimals)
Parameter | Description |
---|---|
x | The input number that needs to be rounded. |
decimals | The number of decimal places. |
public static double roundDecimals(double x, int decimals)
//package com.java2s; /* must be accompanied by the FIRST BSD license file in the root directory of */ public class Main { /**//from w w w. ja v a 2 s .c o m * Rounds a double to specified number of decimal places. * @param x The input number that needs to be rounded. * @param decimals The number of decimal places. * @return The input number rounded to the number of decimal places specified. */ public static double roundDecimals(double x, int decimals) { if (decimals > 9) return (Math.ceil(x * decimals) / decimals); double z = 1; for (int i = 1; i <= decimals; i++) z *= 10; return (Math.ceil(x * z) / z); } }