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 8中将List转换为Map对象方法

    在Java 8中将List转换为Map对象方法

    这篇文章主要介绍了在Java 8中将List转换为Map对象方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-11-11
  • 用html css javascript打造自己的RIA图文教程

    用html css javascript打造自己的RIA图文教程

    用html&css&javascript打造自己的RIA之一,包括了配置等
    2009-07-07
  • java编译器的基础知识点

    java编译器的基础知识点

    在本篇文章里小编给大家整理的是一篇关于java编译器的基础知识点内容,有兴趣的朋友们可以阅读下。
    2020-02-02
  • 关于Java中Bean的生命周期详解

    关于Java中Bean的生命周期详解

    这篇文章主要介绍了关于Java中Bean的生命周期详解,所谓的⽣命周期指的是⼀个对象从诞⽣到销毁的整个⽣命过程,我们把这个过程就叫做⼀个对象的⽣命周期,需要的朋友可以参考下
    2023-08-08
  • springboot整合easy-es实现数据的增删改查的示例代码

    springboot整合easy-es实现数据的增删改查的示例代码

    Easy-Es是一款基于ElasticSearch官方提供的RestHighLevelClient打造的低码开发框架,本文主要介绍了springboot整合easy-es实现数据的增删改查的示例代码,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • 浅谈一下maven优缺点及使用和特点

    浅谈一下maven优缺点及使用和特点

    这篇文章主要介绍了浅谈一下maven优缺点及使用和特点,一个项目管理工具软件,那么maven项目有什么优缺点呢,让我们一起来看看吧
    2023-03-03
  • 基于Java实现获取本地IP地址和主机名

    基于Java实现获取本地IP地址和主机名

    这篇文章主要介绍了基于Java实现获取本地IP地址和主机名,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • SpringBoot+SpringCache实现两级缓存(Redis+Caffeine)

    SpringBoot+SpringCache实现两级缓存(Redis+Caffeine)

    这篇文章主要介绍了SpringBoot+SpringCache实现两级缓存(Redis+Caffeine),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Java基础知识汇总

    Java基础知识汇总

    这篇文章对Java编程语言的基础知识作了一个较为全面的汇总,在这里给大家分享一下。需要的朋友可以参考。
    2017-09-09
  • 超详细的Intellij IDEA 看源码必备技能

    超详细的Intellij IDEA 看源码必备技能

    这篇文章主要介绍了Intellij IDEA 看源码必备技能,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04

最新评论