使用 String.valueOf(double d) - 推荐(最常用)
这是最直接、最推荐的方法,它简洁、可读性高,并且能很好地处理 Double 的 null 值(如果传入的是 Double 对象而不是 double 基本类型)。

(图片来源网络,侵删)
特点:
- 简单直接:代码意图清晰。
- 处理
null:如果参数是Double对象且为null,它会返回字符串"null",而不会抛出NullPointerException。 - 默认格式:使用
Double.toString()的逻辑,通常能提供不错的默认格式。
示例代码:
public class DoubleToStringExample {
public static void main(String[] args) {
double myDouble = 123.456;
Double myDoubleObject = 789.012;
Double nullDouble = null;
// 1. 基本类型 double 转换
String str1 = String.valueOf(myDouble);
System.out.println("String from double: " + str1);
System.out.println("Type of str1: " + str1.getClass().getSimpleName()); // class String
// 2. Double 对象转换
String str2 = String.valueOf(myDoubleObject);
System.out.println("String from Double object: " + str2);
// 3. 处理 null Double 对象
String str3 = String.valueOf(nullDouble);
System.out.println("String from null Double: " + str3); // 输出 "null"
}
}
输出:
String from double: 123.456
Type of str1: String
String from Double object: 789.012
String from null Double: null
使用 Double.toString(double d)
这是 Java 标准库中 Double 类提供的静态方法,它与方法一在核心逻辑上是相同的,但 String.valueOf() 更通用,可以接受各种参数类型,而 Double.toString() 只能接受 double。

(图片来源网络,侵删)
特点:
- 功能明确:专门用于
double到String的转换。 - 无
null处理:因为它只接受double基本类型,所以不存在null的问题。
示例代码:
public class DoubleToStringExample2 {
public static void main(String[] args) {
double myDouble = 3.14159265359;
String str = Double.toString(myDouble);
System.out.println("Using Double.toString(): " + str);
}
}
输出:
Using Double.toString(): 3.14159265359
使用 Double 对象的 .toString() 方法
如果你已经有一个 Double 对象,可以直接调用它的 toString() 实例方法。

(图片来源网络,侵删)
特点:
- 面向对象:适用于
Double对象实例。 - 无
null处理:Double对象本身是null,调用.toString()会直接抛出NullPointerException。
示例代码:
public class DoubleToStringExample3 {
public static void main(String[] args) {
Double myDoubleObject = 2.71828;
// 直接调用 Double 对象的 toString() 方法
String str = myDoubleObject.toString();
System.out.println("Using Double object's toString(): " + str);
}
}
输出:
Using Double object's toString(): 2.71828
使用 String.format() 或 PrintWriter.printf() - 格式化控制
当你需要精确控制输出格式时(保留指定小数位数、使用科学计数法等),String.format() 是最佳选择,它使用 C 语言风格的格式化说明符。
特点:
- 高度可控:可以指定小数位数、宽度、对齐方式等。
- 灵活性高:可以轻松集成到更大的格式化字符串中。
常用格式说明符:
f:定点十进制。e或E:科学计数法。g或G:根据数值大小自动选择f或e格式。.%nf:保留n位小数。
示例代码:
public class DoubleToStringFormatting {
public static void main(String[] args) {
double price = 19.99;
double pi = 3.14159265359;
double veryLargeNumber = 1.23e10;
// 1. 保留两位小数
String formattedPrice = String.format("Price: $%.2f", price);
System.out.println(formattedPrice); // Price: $19.99
// 2. 保留四位小数
String formattedPi = String.format("Pi (4 decimals): %.4f", pi);
System.out.println(formattedPi); // Pi (4 decimals): 3.1416
// 3. 使用科学计数法
String scientific = String.format("Large number: %.2e", veryLargeNumber);
System.out.println(scientific); // Large number: 1.23E+10
// 4. 自动选择最佳格式
String autoFormat = String.format("Auto format: %g", pi);
System.out.println(autoFormat); // Auto format: 3.14159
}
}
输出:
Price: $19.99
Pi (4 decimals): 3.1416
Large number: 1.23E+10
Auto format: 3.14159
使用 DecimalFormat - 更复杂的本地化格式化
DecimalFormat 是 java.text 包中的一个类,它提供了比 String.format() 更强大、更灵活的格式化功能,特别是处理本地化(如千位分隔符、小数点符号等)时。
特点:
- 本地化支持:可以轻松适配不同地区的数字格式习惯(欧洲用逗号作小数点)。
- 模式化:通过定义格式化模式字符串来控制输出。
- 可重用:
DecimalFormat实例可以被创建并重复使用。
示例代码:
import java.text.DecimalFormat;
public class DoubleToStringDecimalFormat {
public static void main(String[] args) {
double number = 1234567.8912;
// 创建 DecimalFormat 对象,定义模式
// # 表示一个数字,0 表示一个必须存在的数字(用于补位)
// , 表示千位分隔符
DecimalFormat df1 = new DecimalFormat("#,###.##");
System.out.println("With thousands separator: " + df1.format(number)); // 1,234,567.89
DecimalFormat df2 = new DecimalFormat("0,000.0000");
System.out.println("With padding: " + df2.format(number)); // 1,234,567.8912
// 科学计数法
DecimalFormat df3 = new DecimalFormat("0.###E0");
System.out.println("Scientific notation: " + df3.format(number)); // 1.235E6
}
}
输出:
With thousands separator: 1,234,567.89
With padding: 1,234,567.8912
Scientific notation: 1.235E6
总结与对比
| 方法 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
String.valueOf() |
最简单、最常用、可读性高、能处理 null |
格式固定,无法自定义 | 通用转换,无需特殊格式 |
Double.toString() |
功能明确,直接 | 只能处理 double 基本类型 |
明确知道要转换的是 double 基本类型 |
Double.toString() (对象) |
面向对象 | 无法处理 null 对象 |
已经有一个 Double 对象,且确定不为 null |
String.format() |
格式化控制力强、灵活、语法简洁 | 对于复杂本地化支持不如 DecimalFormat |
需要精确控制小数位数、宽度、对齐等 |
DecimalFormat |
功能最强大、支持复杂本地化、可重用 | 相对复杂,需要创建对象 | 需要千位分隔符、特定的小数点符号、非常复杂的模式 |
最佳实践建议
- 简单转换,无特殊格式要求:首选
String.valueOf(double),它简单、安全、通用。 - 需要精确控制数字格式(如保留两位小数):使用
String.format(),它的语法非常直观,能满足大部分格式化需求。 - 需要复杂的本地化格式(如千位分隔符、货币符号等):使用
DecimalFormat,它是处理此类需求的专家级工具。 - 避免直接拼接:不要使用
"" + myDouble的方式,虽然它能工作,但这是一种不清晰且效率较低的方式,因为它会先创建一个临时的StringBuilder对象。String.valueOf()是更标准、更清晰的替代方案。
