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

 更新时间:2023年07月05日 15:32:17   作者:小白蹦蹦跳跳  
这篇文章主要给大家介绍了关于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.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

总结

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

相关文章

  • java二进制运算基础知识点详解

    java二进制运算基础知识点详解

    在本文里小编给大家分享了关于java二进制运算基础知识点以及实例代码内容,需要的朋友们参考学习下。
    2019-08-08
  • mybatisPlus实现倒序拼接字符串

    mybatisPlus实现倒序拼接字符串

    这篇文章主要介绍了mybatisPlus实现倒序拼接字符串方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • java 使用线程监控文件目录变化的实现方法

    java 使用线程监控文件目录变化的实现方法

    这篇文章主要介绍了java 使用线程监控文件目录变化的实现方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-10-10
  • 时间处理函数工具分享(时间戳计算)

    时间处理函数工具分享(时间戳计算)

    这篇文章主要介绍了时间处理函数工具,包括得到时间戳、周一、周末、时间更改、时间精确计算等功能
    2014-01-01
  • 如何获取所有spring管理的bean

    如何获取所有spring管理的bean

    这篇文章主要介绍了如何获取所有spring管理的bean,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • springboot解决XSS存储型漏洞问题

    springboot解决XSS存储型漏洞问题

    这篇文章主要介绍了springboot解决XSS存储型漏洞问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • 删除JAVA集合中元素的实现代码

    删除JAVA集合中元素的实现代码

    有时候我们要删除集合中的某些元素,那么就可以参考下面的代码
    2013-07-07
  • SpringBoot整合Redis正确的实现分布式锁的示例代码

    SpringBoot整合Redis正确的实现分布式锁的示例代码

    这篇文章主要介绍了SpringBoot整合Redis正确的实现分布式锁的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Java中Cglib代理类重写逻辑详解

    Java中Cglib代理类重写逻辑详解

    这篇文章主要介绍了Java中Cglib代理类重写逻辑详解,代理类中只会维护NoOp这个回调属性,并不会生成目标类MyService中的noOp所对应的方法,需要的朋友可以参考下
    2023-11-11
  • Java中的lambda和stream实现排序

    Java中的lambda和stream实现排序

    这篇文章主要介绍了Java中的lambda和stream实现排序,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09

最新评论