Java保留两位小数

电玩女神 2023-05-31 03:30 135阅读 0赞

1、创建测试类(Test1.java)

  1. package com.jeff;
  2. import java.text.DecimalFormat;
  3. public class Test1 {
  4. public static void main(String[] args) {
  5. double d = 0;
  6. double d1 = 3.1415926;
  7. System.out.println("方法一:" + new DecimalFormat("0.00").format(d));
  8. System.out.println("方法一:" + new DecimalFormat("0.00").format(d1));
  9. System.out.println("方法二:" + String.format("%.2f", d));
  10. System.out.println("方法二:" + String.format("%.2f", d1));
  11. }
  12. }

2、控制台输出结果
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,135人围观)

还没有评论,来说两句吧...

相关阅读