参考:https://www.cnblogs.com/XT-xutao/p/9882404.html
package com.woyujiezhen.test; import java.math.BigDecimal; import java.text.DecimalFormat; public class BigDecimalTest { private static String formatDecimalPoint(BigDecimal bigDecimal, int decimalPoint) { StringBuilder format = decimalPoint == 0 ? new StringBuilder("#,##0") : new StringBuilder("#,##0."); StringBuilder decimal = new StringBuilder(); for (int i = 0; i < decimalPoint; i++) { decimal.append("0"); } if (bigDecimal == null) { return decimalPoint == 0 ? "0" : "0." + decimal; } if (BigDecimal.ZERO.compareTo(bigDecimal) > 0) { format = new StringBuilder("-").append(format); } DecimalFormat decimalFormat = new DecimalFormat(format.append(decimal).toString()); return decimalFormat.format(bigDecimal); } public static void main(String[] args) { Integer a = null; // BigDecimal bigDecimal1 = new BigDecimal(a); String[] strs = {"12", "211", "3333", "0", "0.123", "00.55", null, "6.666", "88888888888.235555", "-12", "-333333333.2366", "-0.00", "-0.12"}; for (String str : strs) { BigDecimal testDecimal; if (str == null) { testDecimal = null; } else { testDecimal = new BigDecimal(str); } for (int i = 0; i < 4; i++) { System.out.print(formatDecimalPoint(testDecimal, i)); System.out.print(" "); } System.out.println(); } } }
标签:BigDecimal,format,StringBuilder,分节,三位,new,null,bigDecimal From: https://www.cnblogs.com/woyujiezhen/p/16892095.html