1. 格式化数字:根据指定的格式模式
public static String formatNumber(double number, String pattern) {
if (pattern == null || pattern.isEmpty()) {
throw new IllegalArgumentException("格式化模式不能为空");
}
DecimalFormat df = new DecimalFormat(pattern);
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
df.setDecimalFormatSymbols(symbols);
return df.format(number);
}
public static void main(String[] args) {
double number = 1234567.89;
String pattern1 = "#,###.00";
String pattern2 = "###,###.##";
String pattern3 = "0.0000";
System.out.println("Pattern 1: " + formatNumber(number, pattern1));
System.out.println("Pattern 2: " + formatNumber(number, pattern2));
System.out.println("Pattern 3: " + formatNumber(number, pattern3));
}