Here you can find the source of roundToPrecision(double number, int precision)
public static double roundToPrecision(double number, int precision)
//package com.java2s; /**/* ww w . j a v a 2 s .c om*/ * NumericImpl contains logic for numeric data. This implementation is referred to YUI's NumericImpl.js which license under * BSD License. - http://yuilibrary.com/license/ * @author jumperchen * */ public class Main { public static double roundToPrecision(double number, int precision) { double decimalPlaces = Math.pow(10, precision); return Math.round(decimalPlaces * number) / decimalPlaces; } }