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
总结
到此这篇关于Java中double保留两位小数的多种方法的文章就介绍到这了,更多相关Java double保留两位小数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
基于Springboot+Mybatis对数据访问层进行单元测试的方式分享
本文将介绍一种快高效、可复用的解决测试方案——对数据访问层做单元测试,文章通过代码示例介绍的非常详细,具有一定的参考价值,需要的朋友可以参考下2023-07-07SpringBoot Mybatis动态数据源切换方案实现过程
这篇文章主要介绍了SpringBoot+Mybatis实现动态数据源切换方案过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-04-04
最新评论