温馨提示×

java中的decimalformat函数怎么使用

小亿
146
2023-12-21 01:13:00
栏目: 编程语言
Java开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

DecimalFormat类是java.text包中的一个类,用于格式化数字。

用法如下:

  1. 导入DecimalFormat类:

    import java.text.DecimalFormat;
    
  2. 创建DecimalFormat对象:

    DecimalFormat df = new DecimalFormat();
    

    可以通过构造函数传入格式化模式,例如:

    DecimalFormat df = new DecimalFormat("0.00");
    
  3. 设置格式化模式:

    df.applyPattern("0.00");
    
  4. 格式化数字:

    double number = 1234.5678;
    String formattedNumber = df.format(number);
    System.out.println(formattedNumber);
    

    输出结果为:

    1234.57
    

    可以使用format方法将数字格式化为指定模式的字符串。

更多关于DecimalFormat的用法,请参考官方文档:DecimalFormat类文档

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:java中decimalformat函数的用法是什么

0