1. Code:
import java.math.BigDecimal;import java.text.DecimalFormat;import java.text.NumberFormat;public class format { double f = 111231.5585; public void m1() { BigDecimal bg = new BigDecimal(f); double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(f1); } /** * The most convenient conversion of DecimalFormat*/ public void m2() { DecimalFormat df = new DecimalFormat("#.00"); System.out.println(df.format(f)); } /** * The easiest printing of String.format*/ public void m3() { System.out.println(String.format("%.2f", f)); } public void m4() { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); System.out.println(nf.format(f)); } public static void main(String[] args) { format f = new format(); f.m1(); f.m2(); f.m3(); f.m4(); }}2. Output result:
111231.56
111231.56
111231.56
111,231.56
Here we provide a tool class that defines the operation methods such as addition, subtraction, multiplication, division and rounding of floating-point numbers. For reference.
Source file MathExtend.java:
import java.math.BigDecimal;public class MathExtend{ //Default division operation accuracy private static final int DEFAULT_DIV_SCALE = 10; /** * Provides accurate addition operation. * @param v1 * @param v2 * @return The sum of two parameters */ public static double add(double v1, double v2) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.add(b2).doubleValue(); } /** * Provide accurate addition operations* @param v1 * @param v2 * @return The mathematical sum of two parameters, returning in string format */ public static String add(String v1, String v2) { BigDecimal b1 = new BigDecimal(v1); BigDecimal b2 = new BigDecimal(v2); return b1.add(b2).toString(); } /** * Provides accurate subtraction operations. * @param v1 * @param v2 * @return The difference between two parameters*/ public static double subtract(double v1, double v2) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.subtract(b2).doubleValue(); } /** * Provide accurate subtraction operation* @param v1 * @param v2 * @return The mathematical difference between two parameters, returns in string format */ public static String subtract(String v1, String v2) { BigDecimal b1 = new BigDecimal(v1); BigDecimal b2 = new BigDecimal(v2); return b1.subtract(b2).toString(); } /** * Provides accurate multiplication operations. * @param v1 * @param v2 * @return Product of two parameters*/ public static double multiply(double v1, double v2) { BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.multiply(b2).doubleValue(); } /** * Provide accurate multiplication operation* @param v1 * @param v2 * @return The mathematical product of two parameters, returns in string format */ public static String multiply(String v1, String v2) { BigDecimal b1 = new BigDecimal(v1); BigDecimal b2 = new BigDecimal(v2); return b1.multiply(b2).toString(); } /** * Provides (relatively) accurate division operation. When there is no situation of division, it is accurate to * 10 digits after the decimal point, and the subsequent numbers are rounded, and the rounding mode uses ROUND_HALF_EVEN * @param v1 * @param v2 * @return The quotient of the two parameters*/ public static double divide(double v1, double v2) { return divide(v1, v2, DEFAULT_DIV_SCALE); } /** * Provides (relatively) accurate division operation. When there is no end to complete the situation, the scale parameter refers to the accuracy of *, and the subsequent numbers are rounded. The rounding mode uses ROUND_HALF_EVEN * @param v1 * @param v2 * @param scale to indicate that it needs to be accurate to several decimal places. * @return quotient of two parameters*/ public static double divide(double v1,double v2, int scale) { return divide(v1, v2, scale, BigDecimal.ROUND_HALF_EVEN); } /** * Provides (relatively) accurate division operation. When there is no end to complete the situation, the scale parameter refers to the accuracy of *, and the subsequent numbers are rounded. Rounding mode uses user-specified rounding mode* @param v1 * @param v2 * @param scale means that you need to be precise to several digits after the decimal point* @param round_mode means user-specified rounding mode* @return quotient of two parameters*/ public static double divide(double v1,double v2,int scale, int round_mode){ if(scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.divide(b2, scale, round_mode).doubleValue(); } /** * Provides (relatively) accurate division operation, when there is endless division, it is accurate to * 10 digits after the decimal point, and the subsequent numbers are rounded, and the rounding mode uses ROUND_HALF_EVEN * @param v1 * @param v2 * @return The quotient of the two parameters, returns in string format */ public static String divide(String v1, String v2) { return divide(v1, v2, DEFAULT_DIV_SCALE); } /** * Provides (relatively) accurate division operations. When there is no end to complete the situation, the scale parameter refers to the accuracy of *, and the subsequent numbers are rounded. The rounding mode uses ROUND_HALF_EVEN * @param v1 * @param v2 * @param scale represents the quotient of the two parameters that need to be accurate to the decimal point * @return The quotient of the two parameters, returning */ public static String divide(String v1, String v2, int scale) { return divide(v1, v2, DEFAULT_DIV_SCALE, BigDecimal.ROUND_HALF_EVEN); } /** * Provides (relatively) accurate division operation. When there is no end to complete the situation, the scale parameter refers to the accuracy of *, and the subsequent numbers are rounded. Rounding mode uses the user-specified rounding mode* @param v1 * @param v2 * @param scale means that the number of digits after the decimal point needs to be accurate to the decimal point* @param round_mode means the user-specified rounding mode* @return The quotient of the two parameters, returned in string format */ public static String divide(String v1, String v2, int scale, int round_mode) { if(scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b1 = new BigDecimal(v1); BigDecimal b2 = new BigDecimal(v2); return b1.divide(b2, scale, round_mode).toString(); } /** * Provides accurate rounding of decimal places, and the rounding mode uses ROUND_HALF_EVEN * @param v Numbers that need to be rounded* @param scale Several digits retained after the decimal place* @return Results after rounding*/ public static double round(double v,int scale) { return round(v, scale, BigDecimal.ROUND_HALF_EVEN); } /** * Provides accurate rounding of decimal places* @param v Numbers that need to be rounded* @param scale How many digits are retained after the decimal point * @param round_mode Specified rounding mode * @return Results after rounding */ public static double round(double v, int scale, int round_mode) { if(scale<0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); return b.setScale(scale, round_mode).doubleValue(); } /** * Provides accurate rounding of decimal places, the rounding mode uses ROUND_HALF_EVEN * @param v Numbers that need to be rounded* @param scale How many digits are retained after the decimal place * @return The result after rounding is returned in string format */ public static String round(String v, int scale) { return round(v, scale, BigDecimal.ROUND_HALF_EVEN); } /** * Provides accurate rounding of decimal places* @param v Numbers that need to be rounded* @param scale How many digits are retained after the decimal place * @param round_mode Specified rounding* @return The result after rounding is returned in string format */ public static String round(String v, int scale, int round_mode) { if(scale<0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(v); return b.setScale(scale, round_mode).toString(); }}