迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 中格式化双精度值

作者:迹忆客 最近更新:2023/09/17 浏览次数:

本文介绍了如何在 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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便