java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java double保留两位小数

Java中double保留两位小数的多种方法

作者:小白蹦蹦跳跳

这篇文章主要给大家介绍了关于Java中double保留两位小数的多种方法,对于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 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

总结

到此这篇关于Java中double保留两位小数的多种方法的文章就介绍到这了,更多相关Java double保留两位小数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文