Java 中格式化双精度值
本文介绍了如何在 Java 中格式化双精度类型值。
Java 中有多种格式化双精度值的方法,例如 DecimalFormat
类,printf()
方法,format()
方法,String.format()
方法等。让我们仔细看一下例子。
在 Java 中使用 DecimalFormat
类格式化 Double
值
在此示例中,我们使用 DecimalFormat
类将指定类型的双精度类型值格式化。例如,要将双精度值格式化为小数点后三个位,我们使用 format()
方法并传递 DecimalFormat
构造函数的格式样式。请参见下面的示例。
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class SimpleTesting
{
public static void main(String[] args)
{
double dVal = 20.23;
System.out.println("Double Value: "+dVal);
String format = "0.000";
NumberFormat formatter = new DecimalFormat(format);
String newDVal = formatter.format(dVal);
System.out.println("Value After Formatting: "+newDVal);
}
}
输出:
String value: 123
Float value: 123.0
在 Java 中使用 format
方法将 Double
值格式化
这是最简单的示例之一,在这里我们需要使用 System
类的 format()
方法而不是 print()
来格式化 double 类型值。此方法用作 printf()
方法,并将格式化的输出打印到控制台。请参见下面的示例。
public class SimpleTesting
{
public static void main(String[] args)
{
double dVal = 20.23;
System.out.println("Double Value: "+dVal);
System.out.format("Value after Formatting: %.3f", dVal);
}
}
输出:
Double Value: 20.23
Value after Formatting: 20.230
在 Java 中使用 printf
方法格式化 Double
值
Java 在 System 类中提供了 printf()
方法,该方法可用于将格式化的输出打印到控制台。我们将 .2
用作两个小数点,并将 .3
用作三个小数点。请参见以下示例。
public class SimpleTesting
{
public static void main(String[] args)
{
double dVal = 20.23;
System.out.println("Double Value: "+dVal);
System.out.printf("Value after Formatting: %.2f", dVal);
System.out.printf("\nValue after Formatting: %.3f", dVal);
}
}
输出:
Double Value: 20.23
Value after Formatting: 20.23
Value after Formatting: 20.230
在 Java 中使用 format
方法对 double
值进行格式化
String
类包含用于获取 Java 中格式化的 String
的方法 format()
。我们使用这些方法来格式化 Java 应用程序中的 double 类型值。请参见下面的示例。
public class SimpleTesting
{
public static void main(String[] args)
{
double dVal = 20.23;
System.out.println("Double Value: "+dVal);
String val1 = String.format("%.2f", dVal);
String val2 = String.format("%.3f", dVal);
System.out.println("Value after Formatting: "+val1);
System.out.println("Value after Formatting: "+val2);
}
}
输出:
Double Value: 20.23
Value after Formatting: 20.23
Value after Formatting: 20.230
在 Java 中使用 String.format()
方法格式化 double
值
String.format()
方法允许再有一项功能使用分隔符(例如,逗号(,
))来将 double 值格式化为数千,数百万等。请参见以下示例。
public class SimpleTesting
{
public static void main(String[] args)
{
double dVal = 2000.23;
System.out.println("Double Value: "+dVal);
String val1 = String.format("$%,.2f", dVal);
String val2 = String.format("$%,.3f", dVal);
System.out.println("Value after Formatting: "+val1);
System.out.println("Value after Formatting: "+val2);
}
}
输出:
Double Value: 2000.23
Value after Formatting: $2,000.23
Value after Formatting: $2,000.230
在 Java 中使用 DecimalFormat
类格式化 double
值
我们使用 NumberFormat
类的 DecimalFormat
类和 getCurrencyInstance()
方法为货币实例创建双精度型值。我们使用 setMinimumFractionDigits()
方法指定双精度类型值中小数点后的位数。请参见下面的示例。
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class SimpleTesting
{
public static void main(String[] args)
{
double dVal = 2000.23;
System.out.println("Double Value: "+dVal);
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance();
decimalFormat.setMinimumFractionDigits(2);
String val1 = decimalFormat.format(dVal);
System.out.println("Value after Formatting: "+val1);
decimalFormat.setMinimumFractionDigits(3);
String val2 = decimalFormat.format(dVal);
System.out.println("Value after Formatting: "+val2);
}
}
输出:
Double Value: 2000.23
Value after Formatting: $2,000.23
Value after Formatting: $2,000.230
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。