一、代码和调试结果
1.1 BigDecimal
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/fa36749de8124266a730817710fdf737.png)
1.2 DecimalFormat
1.3 NumberFormat
二、原代码
BigDecimalUtil.java 代码
package utils;import java.math.BigDecimal;
import java.math.RoundingMode;public class BigDecimalUtil {// 默认除法运算精度private static final int DEFAULT_DIV_SCALE = 5;/*** 提供精确的加法运算** @param v1* @param v2* @return*/public static double add(double v1, double v2) {BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.add(b2).doubleValue();}public static String add(String v1, String v2) {BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.add(b2).toString();}/*** 提供精确的减法运算** @param v1* @param v2* @return*/public static double subtract(double v1, double v2) {BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.subtract(b2).doubleValue();}public static String substract(String v1, String v2) {BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.subtract(b2).toString();}/*** 提供精确的乘法运算** @param v1* @param v2* @return*/public static double multiply(double v1, double v2) {BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.multiply(b2).doubleValue();}public static String multiply(String v1, String v2) {BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.multiply(b2).toString();}/*** 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 小数点以后10位,以后的数字四舍五入,舍入模式采用ROUND_HALF_UP** @param v1* @param v2* @return 两个参数的商*/public static double divide(double v1, double v2) {return divide(v1, v2, DEFAULT_DIV_SCALE);}/*** 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。舍入模式采用ROUND_HALF_UP** @param v1* @param v2* @param scale* 表示需要精确到小数点以后几位。* @return 两个参数的商*/public static double divide(double v1, double v2, int scale) {return divide(v1, v2, scale, RoundingMode.HALF_UP);}/*** 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。舍入模式采用用户指定舍入模式** @param v1* @param v2* @param scale* 表示需要精确到小数点以后几位* @param round_mode* 表示用户指定的舍入模式* @return 两个参数的商*/public static double divide(double v1, double v2, int scale, RoundingMode 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();}/*** 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 小数点以后10位,以后的数字四舍五入,舍入模式采用ROUND_HALF_EVEN** @param v1* @param v2* @return 两个参数的商,以字符串格式返回*/public static String divide(String v1, String v2) {return divide(v1, v2, DEFAULT_DIV_SCALE);}/*** 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。舍入模式采用ROUND_HALF_UP** @param v1* @param v2* @param scale* 表示需要精确到小数点以后几位* @return 两个参数的商,以字符串格式返回*/public static String divide(String v1, String v2, int scale) {return divide(v1, v2, scale, RoundingMode.HALF_UP);}/*** 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。舍入模式采用用户指定舍入模式** @param v1* @param v2* @param scale* 表示需要精确到小数点以后几位* @param round_mode* 表示用户指定的舍入模式* @return 两个参数的商,以字符串格式返回*/public static String divide(String v1, String v2, int scale, RoundingMode 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();}/*** 提供精确的小数位四舍五入处理,舍入模式采用ROUND_HALF_EVEN** @param v* 需要四舍五入的数字* @param scale* 小数点后保留几位* @return 四舍五入后的结果*/public static double round(double v, int scale) {return round(v, scale, RoundingMode.HALF_UP);}/*** 提供精确的小数位四舍五入处理** @param v* 需要四舍五入的数字* @param scale* 小数点后保留几位* @param round_mode* 指定的舍入模式* @return 四舍五入后的结果*/public static double round(double v, int scale, RoundingMode 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();}/*** 提供精确的小数位四舍五入处理,舍入模式采用ROUND_HALF_UP** @param v* 需要四舍五入的数字* @param scale* 小数点后保留几位* @return 四舍五入后的结果,以字符串格式返回*/public static String round(String v, int scale) {return round(v, scale, RoundingMode.HALF_UP);}/*** 提供精确的小数位四舍五入处理** @param v* 需要四舍五入的数字* @param scale* 小数点后保留几位* @param round_mode* 指定的舍入模式* @return 四舍五入后的结果,以字符串格式返回*/public static String round(String v, int scale, RoundingMode 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();}
}
TestBigDecimal.java代码
package test;import utils.BigDecimalUtil;
import utils.PrintlnUtils;import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;public class TestBigDecimal {public static void main(String[] args) {test1();test2();}public static void test1() {BigDecimal a = new BigDecimal("10");BigDecimal b = new BigDecimal("5");BigDecimal c = null;/*RoundingMode.HALF_UP5.5 62.5 31.6 21.1 11.0 1-1.0 -1-1.1 -1-1.6 -2-2.5 -3-5.5 -6------------------------------BigDecimal.ROUND_CEILING5.5 62.5 31.6 21.1 21.0 1-1.0 -1-1.1 -1-1.6 -1-2.5 -2-5.5 -5*///1.加法-add
// c = a.add(b);//c = 15
// c = a.add(b).setScale(2,BigDecimal.ROUND_HALF_UP) // 过时了c = a.add(b).setScale(2, RoundingMode.HALF_UP);// 2 表示:保留两位小数PrintlnUtils.println("c = " + c);//c = 15.00//2.减法-subtractc = a.subtract(b).setScale(2, RoundingMode.HALF_UP);PrintlnUtils.println("c = " + c);// c = 5.00String substract = BigDecimalUtil.substract("10", "5");PrintlnUtils.println("BigDecimalUtil.substract = " + substract);// BigDecimalUtil.substract = 5String substract2 = BigDecimalUtil.substract("10.00", "5");PrintlnUtils.println("BigDecimalUtil.substract = " + substract2);// BigDecimalUtil.substract = 5.00String substract3 = BigDecimalUtil.substract("10.0", "5");PrintlnUtils.println("BigDecimalUtil.substract = " + substract3);// BigDecimalUtil.substract = 5.0String substract4 = BigDecimalUtil.substract("10.0", "5.00");PrintlnUtils.println("BigDecimalUtil.substract = " + substract4);// BigDecimalUtil.substract = 5.00String substract5 = BigDecimalUtil.substract("10", "5.0");PrintlnUtils.println("BigDecimalUtil.substract = " + substract5);// BigDecimalUtil.substract = 5.0//乘法-multiplyc = a.multiply(b).setScale(2, RoundingMode.HALF_UP);PrintlnUtils.println("c = " + c);// c = 50.00//除法-dividec = a.divide(b).setScale(2, RoundingMode.HALF_UP);PrintlnUtils.println("c = " + c);//c = 2.00c = a.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);PrintlnUtils.println("c = " + c);//c = 0.10//整数舍弃零DecimalFormat df = new DecimalFormat("###.##");String s1 = df.format(new BigDecimal(88.88));String s2 = df.format(new BigDecimal(66.00));String s3 = df.format(new BigDecimal(66.15));String s4 = df.format(new BigDecimal(-66.15));String s5 = df.format(new BigDecimal(666.15));String s6 = df.format(new BigDecimal(666.10));String s7 = df.format(new BigDecimal(666.1));String s8 = df.format(new BigDecimal(666.5));String s9 = df.format(new Double(10666.50));String s92 = df.format(new Double(10666.52));String s93 = df.format(new Double(10666.3));String s94 = df.format(new Double(10666.00));String s95 = df.format(new Double(10666.5));PrintlnUtils.println("s1 = "+s1);// s1 = 88.88PrintlnUtils.println("s2 = "+s2);// s2 = 66PrintlnUtils.println("s3 = "+s3);// s3 = 66.15PrintlnUtils.println("s4 = "+s4);// s4 = -66.15PrintlnUtils.println("s5 = "+s5);// s5 = 666.15PrintlnUtils.println("s6 = "+s6);// s6 = 666.1PrintlnUtils.println("s7 = "+s7);// s7 = 666.1PrintlnUtils.println("s8 = "+s8);// s8 = 666.5PrintlnUtils.println("s9 = "+s9);// s9 = 10666.5PrintlnUtils.println("s92 = "+s92);// s92 = 10666.52PrintlnUtils.println("s93 = "+s93);// s93 = 10666.3PrintlnUtils.println("s94 = "+s94);// s94 = 10666PrintlnUtils.println("s95 = "+s95);// s95 = 10666.5}public static void test2() {PrintlnUtils.println("test2 ---------------------------------------------------------------------------------------");// BigDecimal// 保留两位小数System.out.println(new BigDecimal(0.2).setScale(2, RoundingMode.HALF_UP).doubleValue());// 0.2System.out.println(new BigDecimal(0.235).setScale(2, RoundingMode.HALF_UP).doubleValue());// 0.23System.out.println(new BigDecimal(0.2351).setScale(2, RoundingMode.HALF_UP).doubleValue());// 0.24System.out.println(new BigDecimal(42).setScale(2, RoundingMode.HALF_UP).doubleValue());// 42.0PrintlnUtils.println(" ---------------------------------------------------------------------------------------");System.out.println(new BigDecimal(0.2).setScale(2, RoundingMode.HALF_UP));// 0.20System.out.println(new BigDecimal(0.235).setScale(2, RoundingMode.HALF_UP));// 0.23System.out.println(new BigDecimal(0.2351).setScale(2, RoundingMode.HALF_UP));// 0.24System.out.println(new BigDecimal(42).setScale(2, RoundingMode.HALF_UP));// 42.00PrintlnUtils.println(" ---------------------------------------------------------------------------------------");// NumberFormat// 保留两位小数,个位无数字填充 0NumberFormat nformat = NumberFormat.getInstance();nformat.setMaximumFractionDigits(2);System.out.println(nformat.format(0.2));// 0.2System.out.println(nformat.format(0.235));// 0.23System.out.println(nformat.format(0.2351));// 0.24System.out.println(nformat.format(42));// 42PrintlnUtils.println(" ---------------------------------------------------------------------------------------");// DecimalFormat,是NumberFormat的具体实现子类// 保留两位小数,对应位上无数字填充0DecimalFormat df = new DecimalFormat("#0.00");df.setRoundingMode(RoundingMode.CEILING);System.out.println(df.format(0.2));// 0.20System.out.println(df.format(0.235));// 0.23System.out.println(df.format(0.2351));// 0.24, 因为0.2351在0.23-0.24之间,距离0.24更近,所以输出0.24System.out.println(df.format(42));// 42.00PrintlnUtils.println(" ---------------------------------------------------------------------------------------");DecimalFormat df4 = new DecimalFormat();// #:位置上无数字不显示df4.applyPattern("#.##");System.out.println(df4.format(345235.0));// 345235System.out.println(df4.format(345235.01245));// 345235System.out.println(df4.format(345235.0156));// 345235System.out.println(df4.format(345235.425));// 345235PrintlnUtils.println(" ---------------------------------------------------------------------------------------");DecimalFormat df42 = new DecimalFormat("0.00");System.out.println(df42.format(345235.0));System.out.println(df42.format(345235.01245));System.out.println(df42.format(345235.0156));System.out.println(df42.format(345235.425));//345235.00//345235.01//345235.02//345235.42PrintlnUtils.println(" ---------------------------------------------------------------------------------------");df42.setRoundingMode(RoundingMode.CEILING);System.out.println(df42.format(345235.0));System.out.println(df42.format(345235.01245));System.out.println(df42.format(345235.0156));System.out.println(df42.format(345235.425));//345235.00//345235.02//345235.02//345235.43PrintlnUtils.println(" ---------------------------------------------------------------------------------------");// 0:位置上无数字显示0df4.applyPattern("0.00");System.out.println(df4.format(345235.0));// 345235.00System.out.println(df4.format(345235));// 345235.00// 加负数显示df4.applyPattern("-0.00");System.out.println(df4.format(345235.34567));// -345235.35// 逗号分隔df4.applyPattern("-0,000.00");System.out.println(df4.format(345235.34567));// -345,235.35// 百分位df4.applyPattern("0.00%");System.out.println(df4.format(0.34567));// 34.57%// 千分位df4.applyPattern("0.00\u2030");System.out.println(df4.format(0.34567));// 345.67‰// 科学计数法,E之前是底数的格式,E之后的是指数的格式df4.applyPattern("0.00E00");System.out.println(df4.format(2342.444));// 2.34E03// 格式后面加单位符号df4.applyPattern("0.00 KG");System.out.println(df4.format(2342.444));// 2342.44 KG// 格式前面加单位符号df4.applyPattern("$ 0.00");System.out.println(df4.format(2342.444));// $ 2342.44df4.applyPattern("0.00 QA");System.out.println(df4.format(2342.444));// 2342.44 QA// 使用舍入模式:ROUND_HALF_EVEN,// 保留位数是奇数,使用ROUND_HALF_DOWN// 保留位数是偶数,使用ROUND_HALF_UP
// df4.setRoundingMode(RoundingMode.HALF_UP);System.out.println(df4.format(2342.435));// 2342.43 QASystem.out.println(df4.format(2342.445));// 2342.45 QA// String.format// 保留两位小数,个位数及小数点后两位无数字填充0,四舍五入System.out.println(String.format("%.2f", 0.2));// 0.20System.out.println(String.format("%.2f", 0.235));// 0.24System.out.println(String.format("%.2f", 0.236));// 0.24System.out.println(String.format("%.2f", 42.0));// 42.00}
}