实验04——java保留小数的两种方法、字符串转数值

package cn.tedu.demo;

import java.text.DecimalFormat;

/**
 * @author 赵瑞鑫 E-mail:1922250303@qq.com
 * @version 1.0
 * @创建时间:2020年7月16日 下午2:17:16
 * @类说明:
 */
public class Demo7 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //保留小数的两种方法
        double d = 45.123123123;
        System.out.println(String.format("%.3f", d));
        
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println(df.format(d));
        
        //字符串转数值
        String str = df.format(d);//str 变成double值
        double d1 = Double.parseDouble(str);
        System.out.println(str);
    }

}
作者:赵瑞鑫。支持原创,从你我做起。
原文地址:https://www.cnblogs.com/Winer-Jiu/p/13393397.html