数据格式解决

来源:互联网 发布:按键宏软件 编辑:程序博客网 时间:2024/05/01 05:33

/**
 * 任务:
 * 对像123.356489这样的数
 * 保留小数后n位(含四舍五入)
 * @author Administrator
 */

import java.text.DecimalFormat;

public class NumFormatDemo {

 public static void main(String[] args) {
  double x = 123.356489;
  System.out.println("x = " + x);
  //实现方法1
  System.out.printf("Result  = %.3f/n", x);
  //实现方法2
  DecimalFormat df = new DecimalFormat("##0.00");
  System.out.println("Result = " + df.format(x));
 }

}

原创粉丝点击