How do I format a number in Java?

前端 未结 9 1773
自闭症患者
自闭症患者 2020-11-22 05:10

How do I format a number in Java?
What are the "Best Practices"?

Will I need to round a number before I format it?

32.302342

9条回答
  •  太阳男子
    2020-11-22 05:16

    public static void formatDouble(double myDouble){
     NumberFormat numberFormatter = new DecimalFormat("##.000");
     String result = numberFormatter.format(myDouble);
     System.out.println(result);
    }
    

    For instance, if the double value passed into the formatDouble() method is 345.9372, the following will be the result: 345.937 Similarly, if the value .7697 is passed to the method, the following will be the result: .770

提交回复
热议问题