Java中double保留两位小数

方法一:格式化输出

double one=5;
System.out.printf("%.02f", one);  //5.00

方法二:BigDecimal.setScale()

BigDecimal.setScale()这个方法是用于格式化小数点

setScale(0)表示保留整数
setScale(1)表示保留一位小数,默认用四舍五入
setScale(1,BigDecimal.ROUND_DOWN)直接删除多余的小数位,如2.35会变成2.3
setScale(1,BigDecimal.ROUND_UP)进位处理,2.35变成2.4
setScale(1,BigDecimal.ROUND_HALF_UP)四舍五入,2.35变成2.4
setScale(1,BigDecimal.ROUND_HALF_DOWN)四舍五入,2.35变成2.3,如果是5则向下舍
BigDecimal num1 = new BigDecimal(2.225667); //这种写法不允许,会造成精度损失
BigDecimal num = new BigDecimal(“2.225667”); //一般都会这样写最好

double one1 = 5.459;
BigDecimal two1 = new BigDecimal(one1);
double three1 = two1.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(three1);  //5.46

方法三:DecimalFormat

double one2 = 5.459;
DecimalFormat format2 = new DecimalFormat("#.00");
String str2 = format2.format(one2);
System.out.println(str2);  //5.46
double four2 = Double.parseDouble(str2);
System.out.println(four2);  //5.46

方法四:String.format

double one3 = 5.459;
String  str3 = String.format("%.2f",one3);
System.out.println(str3);  //5.46
double four3 = Double.parseDouble(str3);
System.out.println(four3);  //5.46

方法五:NumberFormat

double one4 = 5.459;
NumberFormat format4 = NumberFormat.getInstance();
format4.setMaximumFractionDigits(2);
String  str4 = format4.format(one4);
System.out.println(str4);  //5.46
double two4 = Double.parseDouble(str4);
System.out.println(two4);  //5.46

补充:

  • Double.valueOf(String s)返回保存用参数字符串s表示的double值的Double对象
  • Double.parseDouble返回一个初始化为指定String表示的值的新double
  • 如果要变成int,则用Integer.parseInt()==

方法二到四遇到的问题:

不能将double 5转换成double 5.00输出

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class test1 {
    public static void main(String[] args) {

        double one1 = 5;
        BigDecimal two1 = new BigDecimal(one1);
        double three1 = two1.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(three1);  //5.0

        double one2 = 5;
        DecimalFormat format2 = new DecimalFormat("#.00");
        String str2 = format2.format(one2);
        System.out.println(str2);  //5.00
        double four2 = Double.parseDouble(str2);
        System.out.println(four2);  //5.0

        double one3 = 5;
        String  str3 = String.format("%.2f",one3);
        System.out.println(str3);  //5.00
        double four3 = Double.parseDouble(str3);
        System.out.println(four3);  //5.0

        double one4 = 5;
        NumberFormat format4 = NumberFormat.getInstance();
        format4.setMaximumFractionDigits(2);
        String  str4 = format4.format(one4);
        System.out.println(str4);  //5
        double two4 = Double.parseDouble(str4);
        System.out.println(two4);  //5.0
    }
}
package code;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class test1 {
    public static void main(String[] args) {

        double one1 = 5.0;
        BigDecimal two1 = new BigDecimal(one1);
        double three1 = two1.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(three1);  //5.0

        double one2 = 5.0;
        DecimalFormat format2 = new DecimalFormat("#.00");
        String str2 = format2.format(one2);
        System.out.println(str2);  //5.00
        double four2 = Double.parseDouble(str2);
        System.out.println(four2);  //5.0

        double one3 = 5.0;
        String  str3 = String.format("%.2f",one3);
        System.out.println(str3);  //5.00
        double four3 = Double.parseDouble(str3);
        System.out.println(four3);  //5.0

        double one4 = 5.0;
        NumberFormat format4 = NumberFormat.getInstance();
        format4.setMaximumFractionDigits(2);
        String  str4 = format4.format(one4);
        System.out.println(str4);  //5
        double two4 = Double.parseDouble(str4);
        System.out.println(two4);  //5.0
    }
}
package code;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class test1 {
    public static void main(String[] args) {

        double one1 = 5.00;
        BigDecimal two1 = new BigDecimal(one1);
        double three1 = two1.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(three1);  //5.0
        
        double one2 = 5.00;
        DecimalFormat format2 = new DecimalFormat("#.00");
        String str2 = format2.format(one2);
        System.out.println(str2);  //5.00
        double four2 = Double.parseDouble(str2);
        System.out.println(four2);  //5.0

        double one3 = 5.00;
        String  str3 = String.format("%.2f",one3);
        System.out.println(str3);  //5.00
        double four3 = Double.parseDouble(str3);
        System.out.println(four3);  //5.0

        double one4 = 5.00;
        NumberFormat format4 = NumberFormat.getInstance();
        format4.setMaximumFractionDigits(2);
        String  str4 = format4.format(one4);
        System.out.println(str4);  //5
        double two4 = Double.parseDouble(str4);
        System.out.println(two4);  //5.0
    }
}

不管是5或者是5.000…最后的结果都是5.0

你可能感兴趣的:(学习笔记,1024程序员节)