活动公告

系统通知
05-18 21:22
系统通知
通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,资源失效请在帖子内回复要求补档,会尽快处理!
10-23 09:31

Java中输出double类型数据的完整指南从基础Systemoutprintln到DecimalFormat格式化详解

SunJu_FaceMall

3万

主题

2860

科技点

3万

积分

白金月票

碾压王

积分
32872

塔罗立华奏

<font color=白金月票" /> 发表于 2025-9-14 16:20:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1. Java中double类型简介

在Java中,double是64位双精度浮点数类型,是表示小数的主要数据类型之一。它可以表示的范围大约是±4.94065645841246544e-324到±1.79769313486231570e+308,精度为15-16位有效数字。在处理科学计算、金融数据或其他需要高精度小数的场景中,double类型被广泛使用。
  1. double pi = 3.141592653589793;
  2. double price = 19.99;
  3. double scientificNumber = 1.23e10; // 科学计数法表示12300000000.0
复制代码

2. 使用System.out.println输出double类型数据

2.1 基本输出

最简单的输出double类型数据的方法是使用System.out.println():
  1. public class BasicDoubleOutput {
  2.     public static void main(String[] args) {
  3.         double value = 3.141592653589793;
  4.         System.out.println(value); // 输出: 3.141592653589793
  5.         
  6.         double price = 19.99;
  7.         System.out.println(price); // 输出: 19.99
  8.         
  9.         double largeNumber = 123456789.987654321;
  10.         System.out.println(largeNumber); // 输出: 1.2345678998765432E8
  11.     }
  12. }
复制代码

2.2 与字符串拼接输出

可以将double类型数据与字符串拼接输出:
  1. public class StringConcatenation {
  2.     public static void main(String[] args) {
  3.         double pi = 3.14159;
  4.         System.out.println("圆周率的值是: " + pi); // 输出: 圆周率的值是: 3.14159
  5.         
  6.         double price = 19.99;
  7.         System.out.println("商品价格: $" + price); // 输出: 商品价格: $19.99
  8.     }
  9. }
复制代码

2.3 输出多个double值

可以在一个println语句中输出多个double值:
  1. public class MultipleDoubles {
  2.     public static void main(String[] args) {
  3.         double length = 10.5;
  4.         double width = 5.25;
  5.         System.out.println("长度: " + length + ", 宽度: " + width);
  6.         // 输出: 长度: 10.5, 宽度: 5.25
  7.         
  8.         // 计算面积并输出
  9.         double area = length * width;
  10.         System.out.println("面积: " + area); // 输出: 面积: 55.125
  11.     }
  12. }
复制代码

3. 使用System.out.printf进行格式化输出

3.1 基本格式化输出

printf方法允许我们使用格式说明符来控制输出的格式:
  1. public class PrintfBasic {
  2.     public static void main(String[] args) {
  3.         double pi = 3.141592653589793;
  4.         
  5.         // %f 表示浮点数格式
  6.         System.out.printf("圆周率: %f%n", pi); // 输出: 圆周率: 3.141593
  7.         
  8.         // 指定小数位数
  9.         System.out.printf("圆周率(2位小数): %.2f%n", pi); // 输出: 圆周率(2位小数): 3.14
  10.         System.out.printf("圆周率(4位小数): %.4f%n", pi); // 输出: 圆周率(4位小数): 3.1416
  11.         
  12.         // 指定最小宽度
  13.         System.out.printf("圆周率(宽度10): %10.2f%n", pi); // 输出: 圆周率(宽度10):       3.14
  14.         System.out.printf("圆周率(宽度10,左对齐): %-10.2f%n", pi); // 输出: 圆周率(宽度10,左对齐): 3.14      
  15.     }
  16. }
复制代码

3.2 高级格式化选项
  1. public class PrintfAdvanced {
  2.     public static void main(String[] args) {
  3.         double value = 123.456789;
  4.         
  5.         // 使用逗号分组
  6.         System.out.printf("带分组: %,.2f%n", 1234567.89); // 输出: 带分组: 1,234,567.89
  7.         
  8.         // 科学计数法
  9.         System.out.printf("科学计数法: %e%n", value); // 输出: 科学计数法: 1.234568e+02
  10.         System.out.printf("科学计数法(2位小数): %.2e%n", value); // 输出: 科学计数法(2位小数): 1.23e+02
  11.         
  12.         // 十六进制浮点数
  13.         System.out.printf("十六进制浮点数: %a%n", value); // 输出: 十六进制浮点数: 0x1.edd2f1a9fbe77p6
  14.         
  15.         // 正负号显示
  16.         System.out.printf("带正号: %+f%n", value); // 输出: 带正号: +123.456789
  17.         System.out.printf("带正号: %+f%n", -value); // 输出: 带正号: -123.456789
  18.         
  19.         // 括号表示负数
  20.         System.out.printf("括号表示负数: %(f%n", value); // 输出: 括号表示负数: 123.456789
  21.         System.out.printf("括号表示负数: %(f%n", -value); // 输出: 括号表示负数: (123.456789)
  22.     }
  23. }
复制代码

3.3 多个值格式化
  1. public class PrintfMultiple {
  2.     public static void main(String[] args) {
  3.         double price = 19.99;
  4.         double taxRate = 0.08;
  5.         double tax = price * taxRate;
  6.         double total = price + tax;
  7.         
  8.         System.out.printf("商品价格: $%.2f%n", price);
  9.         System.out.printf("税率: %.1f%%%n", taxRate * 100);
  10.         System.out.printf("税额: $%.2f%n", tax);
  11.         System.out.printf("总价: $%.2f%n", total);
  12.         
  13.         // 一行输出多个值
  14.         System.out.printf("价格: $%.2f, 税率: %.1f%%, 总价: $%.2f%n",
  15.                           price, taxRate * 100, total);
  16.     }
  17. }
复制代码

4. 使用String.format()格式化double

String.format()方法与printf类似,但它返回一个格式化后的字符串,而不是直接输出:
  1. public class StringFormatExample {
  2.     public static void main(String[] args) {
  3.         double pi = 3.141592653589793;
  4.         
  5.         // 基本格式化
  6.         String formatted = String.format("圆周率: %.2f", pi);
  7.         System.out.println(formatted); // 输出: 圆周率: 3.14
  8.         
  9.         // 多个值格式化
  10.         double price = 19.99;
  11.         double discount = 0.15;
  12.         double discountedPrice = price * (1 - discount);
  13.         
  14.         String receipt = String.format("原价: $%.2f, 折扣: %.0f%%, 折后价: $%.2f",
  15.                                       price, discount * 100, discountedPrice);
  16.         System.out.println(receipt); // 输出: 原价: $19.99, 折扣: 15%, 折后价: $16.99
  17.         
  18.         // 在日志或消息中使用
  19.         String logMessage = String.format("[INFO] 计算完成, 结果: %.4f", Math.sqrt(2));
  20.         System.out.println(logMessage); // 输出: [INFO] 计算完成, 结果: 1.4142
  21.     }
  22. }
复制代码

5. 使用DecimalFormat类进行高级格式化

5.1 DecimalFormat基本用法

DecimalFormat是NumberFormat的具体子类,用于格式化十进制数字。它提供了丰富的格式化选项:
  1. import java.text.DecimalFormat;
  2. public class DecimalFormatBasic {
  3.     public static void main(String[] args) {
  4.         double value = 1234.56789;
  5.         
  6.         // 创建DecimalFormat实例
  7.         DecimalFormat df = new DecimalFormat();
  8.         
  9.         // 应用默认格式
  10.         System.out.println("默认格式: " + df.format(value)); // 输出: 默认格式: 1,234.568
  11.         
  12.         // 自定义格式 - 两位小数
  13.         df = new DecimalFormat("0.00");
  14.         System.out.println("两位小数: " + df.format(value)); // 输出: 两位小数: 1234.57
  15.         
  16.         // 自定义格式 - 三位小数
  17.         df = new DecimalFormat("0.000");
  18.         System.out.println("三位小数: " + df.format(value)); // 输出: 三位小数: 1234.568
  19.         
  20.         // 自定义格式 - 整数部分
  21.         df = new DecimalFormat("#");
  22.         System.out.println("整数部分: " + df.format(value)); // 输出: 整数部分: 1235
  23.         
  24.         // 自定义格式 - 千位分隔符
  25.         df = new DecimalFormat("#,###");
  26.         System.out.println("千位分隔符: " + df.format(value)); // 输出: 千位分隔符: 1,235
  27.         
  28.         // 自定义格式 - 千位分隔符和两位小数
  29.         df = new DecimalFormat("#,##0.00");
  30.         System.out.println("千位分隔符和两位小数: " + df.format(value)); // 输出: 千位分隔符和两位小数: 1,234.57
  31.     }
  32. }
复制代码

5.2 DecimalFormat高级模式
  1. import java.text.DecimalFormat;
  2. public class DecimalFormatAdvanced {
  3.     public static void main(String[] args) {
  4.         double value = 1234.5;
  5.         
  6.         // 前缀和后缀
  7.         DecimalFormat df = new DecimalFormat("$#,##0.00");
  8.         System.out.println("货币格式: " + df.format(value)); // 输出: 货币格式: $1,234.50
  9.         
  10.         df = new DecimalFormat("0.00%");
  11.         System.out.println("百分比格式: " + df.format(0.85)); // 输出: 百分比格式: 85.00%
  12.         
  13.         // 科学计数法
  14.         df = new DecimalFormat("0.###E0");
  15.         System.out.println("科学计数法: " + df.format(12345)); // 输出: 科学计数法: 1.235E4
  16.         
  17.         // 正数、负数和零的不同格式
  18.         df = new DecimalFormat("$#,##0.00;($#,##0.00)");
  19.         System.out.println("正数: " + df.format(1234.5)); // 输出: 正数: $1,234.50
  20.         System.out.println("负数: " + df.format(-1234.5)); // 输出: 负数: ($1,234.50)
  21.         
  22.         // 分数表示
  23.         df = new DecimalFormat("# #/#");
  24.         System.out.println("分数表示: " + df.format(0.75)); // 输出: 分数表示: 3 3/4
  25.     }
  26. }
复制代码

5.3 DecimalFormat的舍入模式
  1. import java.text.DecimalFormat;
  2. import java.math.RoundingMode;
  3. public class DecimalFormatRounding {
  4.     public static void main(String[] args) {
  5.         double value = 3.14159;
  6.         
  7.         // 创建DecimalFormat实例
  8.         DecimalFormat df = new DecimalFormat("0.00");
  9.         
  10.         // 设置舍入模式
  11.         df.setRoundingMode(RoundingMode.UP);
  12.         System.out.println("向上舍入: " + df.format(value)); // 输出: 向上舍入: 3.15
  13.         
  14.         df.setRoundingMode(RoundingMode.DOWN);
  15.         System.out.println("向下舍入: " + df.format(value)); // 输出: 向下舍入: 3.14
  16.         
  17.         df.setRoundingMode(RoundingMode.CEILING);
  18.         System.out.println("向正无穷舍入: " + df.format(value)); // 输出: 向正无穷舍入: 3.15
  19.         
  20.         df.setRoundingMode(RoundingMode.FLOOR);
  21.         System.out.println("向负无穷舍入: " + df.format(value)); // 输出: 向负无穷舍入: 3.14
  22.         
  23.         df.setRoundingMode(RoundingMode.HALF_UP);
  24.         System.out.println("四舍五入: " + df.format(value)); // 输出: 四舍五入: 3.14
  25.         
  26.         df.setRoundingMode(RoundingMode.HALF_DOWN);
  27.         System.out.println("五舍六入: " + df.format(value)); // 输出: 五舍六入: 3.14
  28.         
  29.         df.setRoundingMode(RoundingMode.HALF_EVEN);
  30.         System.out.println("银行家舍入法: " + df.format(value)); // 输出: 银行家舍入法: 3.14
  31.         
  32.         // 测试2.5的舍入
  33.         double testValue = 2.5;
  34.         df.setRoundingMode(RoundingMode.HALF_UP);
  35.         System.out.println("2.5四舍五入: " + df.format(testValue)); // 输出: 2.5四舍五入: 2.50
  36.         
  37.         df.setRoundingMode(RoundingMode.HALF_EVEN);
  38.         System.out.println("2.5银行家舍入法: " + df.format(testValue)); // 输出: 2.5银行家舍入法: 2.50
  39.     }
  40. }
复制代码

6. 使用NumberFormat进行本地化格式化

NumberFormat是一个抽象基类,用于格式化数字。它可以根据本地环境自动调整格式:
  1. import java.text.NumberFormat;
  2. import java.util.Locale;
  3. public class NumberFormatExample {
  4.     public static void main(String[] args) {
  5.         double value = 1234567.89;
  6.         
  7.         // 获取默认环境的NumberFormat实例
  8.         NumberFormat nf = NumberFormat.getInstance();
  9.         System.out.println("默认格式: " + nf.format(value));
  10.         
  11.         // 获取特定环境的NumberFormat实例
  12.         NumberFormat usFormat = NumberFormat.getInstance(Locale.US);
  13.         System.out.println("美国格式: " + usFormat.format(value));
  14.         
  15.         NumberFormat germanFormat = NumberFormat.getInstance(Locale.GERMANY);
  16.         System.out.println("德国格式: " + germanFormat.format(value));
  17.         
  18.         NumberFormat frenchFormat = NumberFormat.getInstance(Locale.FRANCE);
  19.         System.out.println("法国格式: " + frenchFormat.format(value));
  20.         
  21.         // 货币格式
  22.         NumberFormat currencyUs = NumberFormat.getCurrencyInstance(Locale.US);
  23.         System.out.println("美国货币: " + currencyUs.format(value));
  24.         
  25.         NumberFormat currencyChina = NumberFormat.getCurrencyInstance(Locale.CHINA);
  26.         System.out.println("中国货币: " + currencyChina.format(value));
  27.         
  28.         NumberFormat currencyJapan = NumberFormat.getCurrencyInstance(Locale.JAPAN);
  29.         System.out.println("日本货币: " + currencyJapan.format(value));
  30.         
  31.         // 百分比格式
  32.         NumberFormat percentFormat = NumberFormat.getPercentInstance();
  33.         System.out.println("百分比: " + percentFormat.format(0.85));
  34.         
  35.         // 设置小数位数
  36.         nf.setMinimumFractionDigits(2);
  37.         nf.setMaximumFractionDigits(4);
  38.         System.out.println("自定义小数位数: " + nf.format(value));
  39.     }
  40. }
复制代码

7. 处理特殊double值

Java中的double类型有一些特殊值,如正无穷大、负无穷大和NaN(非数字),需要特殊处理:
  1. public class SpecialDoubleValues {
  2.     public static void main(String[] args) {
  3.         // 正无穷大
  4.         double positiveInfinity = Double.POSITIVE_INFINITY;
  5.         System.out.println("正无穷大: " + positiveInfinity);
  6.         System.out.printf("正无穷大(格式化): %.2f%n", positiveInfinity);
  7.         
  8.         // 负无穷大
  9.         double negativeInfinity = Double.NEGATIVE_INFINITY;
  10.         System.out.println("负无穷大: " + negativeInfinity);
  11.         System.out.printf("负无穷大(格式化): %.2f%n", negativeInfinity);
  12.         
  13.         // NaN
  14.         double nan = Double.NaN;
  15.         System.out.println("NaN: " + nan);
  16.         System.out.printf("NaN(格式化): %.2f%n", nan);
  17.         
  18.         // 使用DecimalFormat处理特殊值
  19.         java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
  20.         System.out.println("正无穷大(DecimalFormat): " + df.format(positiveInfinity));
  21.         System.out.println("负无穷大(DecimalFormat): " + df.format(negativeInfinity));
  22.         System.out.println("NaN(DecimalFormat): " + df.format(nan));
  23.         
  24.         // 检查特殊值
  25.         double x = 0.0;
  26.         double y = 0.0;
  27.         double result = x / y;
  28.         
  29.         if (Double.isInfinite(result)) {
  30.             System.out.println("结果是无穷大");
  31.         }
  32.         
  33.         if (Double.isNaN(result)) {
  34.             System.out.println("结果是NaN");
  35.         }
  36.     }
  37. }
复制代码

8. 实际应用场景和最佳实践

8.1 金融计算中的格式化
  1. import java.text.DecimalFormat;
  2. import java.text.NumberFormat;
  3. import java.util.Locale;
  4. public class FinancialFormatting {
  5.     public static void main(String[] args) {
  6.         // 货币格式化
  7.         double amount = 1234567.8956;
  8.         
  9.         // 使用DecimalFormat
  10.         DecimalFormat currencyFormat = new DecimalFormat("$#,##0.00");
  11.         System.out.println("金额: " + currencyFormat.format(amount));
  12.         
  13.         // 使用NumberFormat进行本地化货币格式化
  14.         NumberFormat usCurrency = NumberFormat.getCurrencyInstance(Locale.US);
  15.         NumberFormat chinaCurrency = NumberFormat.getCurrencyInstance(Locale.CHINA);
  16.         NumberFormat japanCurrency = NumberFormat.getCurrencyInstance(Locale.JAPAN);
  17.         
  18.         System.out.println("美国货币: " + usCurrency.format(amount));
  19.         System.out.println("中国货币: " + chinaCurrency.format(amount));
  20.         System.out.println("日本货币: " + japanCurrency.format(amount));
  21.         
  22.         // 利率计算和格式化
  23.         double principal = 10000.00;
  24.         double rate = 0.035; // 3.5%
  25.         double interest = principal * rate;
  26.         
  27.         DecimalFormat percentFormat = new DecimalFormat("0.00%");
  28.         DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
  29.         
  30.         System.out.println("本金: " + decimalFormat.format(principal));
  31.         System.out.println("利率: " + percentFormat.format(rate));
  32.         System.out.println("利息: " + decimalFormat.format(interest));
  33.         
  34.         // 股票价格格式化
  35.         double stockPrice = 123.456;
  36.         DecimalFormat stockFormat = new DecimalFormat("0.000");
  37.         System.out.println("股票价格: " + stockFormat.format(stockPrice));
  38.     }
  39. }
复制代码

8.2 科学计算中的格式化
  1. import java.text.DecimalFormat;
  2. public class ScientificFormatting {
  3.     public static void main(String[] args) {
  4.         // 科学计数法
  5.         double avogadro = 6.02214076e23;
  6.         double planck = 6.62607015e-34;
  7.         
  8.         // 使用printf
  9.         System.out.printf("阿伏伽德罗常数: %.4e%n", avogadro);
  10.         System.out.printf("普朗克常数: %.4e%n", planck);
  11.         
  12.         // 使用DecimalFormat
  13.         DecimalFormat scientificFormat = new DecimalFormat("0.###E0");
  14.         System.out.println("阿伏伽德罗常数: " + scientificFormat.format(avogadro));
  15.         System.out.println("普朗克常数: " + scientificFormat.format(planck));
  16.         
  17.         // 工程计数法(指数是3的倍数)
  18.         DecimalFormat engineeringFormat = new DecimalFormat("##0.####E0");
  19.         System.out.println("工程计数法: " + engineeringFormat.format(12345)); // 12.345E3
  20.         
  21.         // 设置有效数字
  22.         double value = 123.456789;
  23.         DecimalFormat significantDigits = new DecimalFormat("@@@@");
  24.         System.out.println("4位有效数字: " + significantDigits.format(value));
  25.     }
  26. }
复制代码

8.3 数据报表中的格式化
  1. import java.text.DecimalFormat;
  2. import java.text.NumberFormat;
  3. public class ReportFormatting {
  4.     public static void main(String[] args) {
  5.         // 创建表头
  6.         System.out.println("----------------------------------------");
  7.         System.out.printf("%-15s %10s %10s%n", "产品", "单价", "销量");
  8.         System.out.println("----------------------------------------");
  9.         
  10.         // 产品数据
  11.         String[] products = {"笔记本电脑", "智能手机", "平板电脑", "智能手表"};
  12.         double[] prices = {5999.99, 3299.50, 2199.00, 1299.75};
  13.         int[] quantities = {120, 250, 80, 300};
  14.         
  15.         // 格式化输出
  16.         NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
  17.         DecimalFormat quantityFormat = new DecimalFormat("#,###");
  18.         
  19.         for (int i = 0; i < products.length; i++) {
  20.             System.out.printf("%-15s %12s %10s%n",
  21.                             products[i],
  22.                             currencyFormat.format(prices[i]),
  23.                             quantityFormat.format(quantities[i]));
  24.         }
  25.         
  26.         System.out.println("----------------------------------------");
  27.         
  28.         // 计算并显示总计
  29.         double totalRevenue = 0;
  30.         int totalQuantity = 0;
  31.         
  32.         for (int i = 0; i < prices.length; i++) {
  33.             totalRevenue += prices[i] * quantities[i];
  34.             totalQuantity += quantities[i];
  35.         }
  36.         
  37.         System.out.printf("%-15s %12s %10s%n",
  38.                         "总计",
  39.                         currencyFormat.format(totalRevenue),
  40.                         quantityFormat.format(totalQuantity));
  41.         System.out.println("----------------------------------------");
  42.         
  43.         // 显示百分比
  44.         System.out.println("\n销量占比:");
  45.         for (int i = 0; i < products.length; i++) {
  46.             double percentage = (double) quantities[i] / totalQuantity * 100;
  47.             System.out.printf("%-15s %10.1f%%%n", products[i], percentage);
  48.         }
  49.     }
  50. }
复制代码

8.4 性能考虑和最佳实践
  1. import java.text.DecimalFormat;
  2. public class PerformanceBestPractices {
  3.     public static void main(String[] args) {
  4.         // 1. 重用DecimalFormat实例
  5.         DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
  6.         
  7.         double[] values = {1234.567, 9876.543, 5555.555, 7777.777};
  8.         
  9.         // 好的做法:重用格式化对象
  10.         for (double value : values) {
  11.             System.out.println("格式化值: " + decimalFormat.format(value));
  12.         }
  13.         
  14.         // 2. 在循环中避免创建新的格式化对象
  15.         long startTime = System.currentTimeMillis();
  16.         
  17.         // 不好的做法:在循环中创建新的格式化对象
  18.         for (int i = 0; i < 100000; i++) {
  19.             DecimalFormat df = new DecimalFormat("#,##0.00");
  20.             df.format(1234.56);
  21.         }
  22.         
  23.         long endTime = System.currentTimeMillis();
  24.         System.out.println("循环中创建DecimalFormat耗时: " + (endTime - startTime) + "ms");
  25.         
  26.         // 好的做法:重用格式化对象
  27.         startTime = System.currentTimeMillis();
  28.         DecimalFormat reusableDf = new DecimalFormat("#,##0.00");
  29.         
  30.         for (int i = 0; i < 100000; i++) {
  31.             reusableDf.format(1234.56);
  32.         }
  33.         
  34.         endTime = System.currentTimeMillis();
  35.         System.out.println("重用DecimalFormat耗时: " + (endTime - startTime) + "ms");
  36.         
  37.         // 3. 线程安全考虑
  38.         // DecimalFormat不是线程安全的,在多线程环境中应该:
  39.         // a) 每个线程创建自己的实例
  40.         // b) 使用同步
  41.         // c) 使用ThreadLocal
  42.         
  43.         // 4. 选择合适的格式化方法
  44.         // 简单输出:使用System.out.println
  45.         // 基本格式化:使用printf或String.format
  46.         // 复杂格式化或本地化:使用DecimalFormat或NumberFormat
  47.         
  48.         // 5. 处理异常
  49.         try {
  50.             // 可能会抛出异常的格式化操作
  51.             String invalidPattern = "#,##0.00#";
  52.             DecimalFormat invalidDf = new DecimalFormat(invalidPattern);
  53.             System.out.println("格式化结果: " + invalidDf.format(1234.56));
  54.         } catch (IllegalArgumentException e) {
  55.             System.err.println("格式化模式错误: " + e.getMessage());
  56.         }
  57.     }
  58. }
复制代码

9. 总结

在Java中输出double类型数据有多种方法,从简单的System.out.println到复杂的DecimalFormat格式化。选择哪种方法取决于具体需求:

1. 简单输出:使用System.out.println,适用于快速调试和简单输出。
2. 基本格式化:使用System.out.printf或String.format,适用于需要控制小数位数、宽度等基本格式化需求的场景。
3. 高级格式化:使用DecimalFormat,适用于需要复杂格式化模式、特定舍入行为或本地化需求的场景。
4. 本地化格式化:使用NumberFormat,适用于需要根据不同地区习惯格式化数字的场景。

在实际应用中,应根据性能要求、线程安全需求和格式化复杂度选择合适的方法。同时,注意处理特殊double值(如无穷大和NaN)以确保程序的健壮性。

通过掌握这些double类型数据的输出和格式化技术,你可以在各种应用场景中灵活地展示数值数据,提高程序的用户体验和可读性。
「七転び八起き(ななころびやおき)」
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则