Here you can find the source of roundToNDigits(double d, int n)
public static double roundToNDigits(double d, int n)
//package com.java2s; //License from project: Apache License public class Main { public static double roundToNDigits(double d, int n) { if (d == 0) return d; int log = (int) Math.log10(d); int exp = n; exp -= log;//from w w w . j a v a 2 s . co m int ival = (int) (Math.round(d * Math.pow(10, exp))); return ival / Math.pow(10, exp); } }