Here you can find the source of round(double d, int n)
public static String round(double d, int n)
//package com.java2s; //License from project: Apache License import java.math.BigDecimal; import java.text.DecimalFormat; public class Main { public static String round(double d, int n) { if (d == 0) { d = 0;// ww w .ja va 2 s. co m } if (n < 0) { n = 0; } String str = ""; if (n == 0) { str = "0"; } else { str = "0."; } for (int i = 0; i < n; i++) { str = str + "0"; } DecimalFormat formater = new DecimalFormat(str); BigDecimal b = new BigDecimal(d + ""); double tempd = b.setScale(n, BigDecimal.ROUND_HALF_UP) .doubleValue(); if (tempd == 0) { tempd = 0; } return formater.format(tempd); } }