Here you can find the source of toFix(double x, int dp)
public static double toFix(double x, int dp)
//package com.java2s; /** This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. *//*from w w w .j a va 2s . com*/ import java.text.*; import java.math.*; public class Main { public static double toFix(double x, int dp) { String format = ""; for (int i = 0; i < dp; i++) { format = format + "#"; } return Double.parseDouble((new DecimalFormat("#0." + format)).format(x)); } public static String toFix(BigDecimal x, int dp) { String format = ""; for (int i = 0; i < dp; i++) { format = format + "#"; } return (new DecimalFormat("#0." + format)).format(x); } }