Java中double类型四舍五入方法

项目要对一个double值保留小数点后3位,网上找到好几种方法,选一个用在项目中,实测发现有误差,再换一种,还是会有误差。最后经过多种情况测试,发现最后2种是精确的。

[java] view plain copy
  1. package com.example.testndk;  
  2.   
  3. import java.math.BigDecimal;  
  4. import java.math.RoundingMode;  
  5. import java.text.DecimalFormat;  
  6. import java.text.NumberFormat;  
  7.   
  8. /** 
  9.  * Created by D.bj on 2017/1/5. 
  10.  */  
  11. public class DoubleTest {  
  12.     /** 
  13.      * 保留3位小数,四舍五入的一个老方法 
  14.      * @param d 
  15.      * @return 
  16.      */  
  17.     public static double formatDouble1(double d) {  
  18.         return (double)Math.round(d*1000)/1000;  
  19.     }  
  20.   
  21.   
  22.     /** 
  23.      * The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. 
  24.      * @param d 
  25.      * @return 
  26.      */  
  27.     public static double formatDouble2(double d) {  
  28.         // 旧方法,已经不再推荐使用  
  29. //        BigDecimal bg = new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP);  
  30.   
  31.         // 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN  
  32.         BigDecimal bg = new BigDecimal(d).setScale(3, RoundingMode.UP);  
  33.         return bg.doubleValue();  
  34.     }  
  35.   
  36.     /** 
  37.      * NumberFormat is the abstract base class for all number formats. 
  38.      * This class provides the interface for formatting and parsing numbers. 
  39.      * @param d 
  40.      * @return 
  41.      */  
  42.     public static String formatDouble3(double d) {  
  43.         NumberFormat nf = NumberFormat.getNumberInstance();  
  44.         // 保留3位小数  
  45.         nf.setMaximumFractionDigits(3);  
  46.         // 如果不需要四舍五入,可以使用RoundingMode.DOWN  
  47.         nf.setRoundingMode(RoundingMode.UP);  
  48.   
  49.         return nf.format(d);  
  50.     }  
  51.   
  52.     /** 
  53.      * DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. 
  54.      * @param d 
  55.      * @return 
  56.      */  
  57.     public static String formatDouble4(double d) {  
  58.         DecimalFormat df = new DecimalFormat("#0.000");  
  59.         return df.format(d);  
  60.     }  
  61.   
  62.   
  63.     /** 
  64.      * 如果只是用于程序中的格式化数值然后输出,那么这个方法还是挺方便的。 
  65.      * 应该是这样使用:System.out.println(String.format("%.3f", d)); 
  66.      * @param d 
  67.      * @return 
  68.      */  
  69.     public static String formatDouble5(double d) {  
  70.         String str = String.format("%.3f", d);  
  71.        // double dscore = Double.parseDouble(str);  
  72.         return str;  
  73.     }  
  74.   
  75.     public static void main(String[] args) {  
  76.         double d = 0.001254;  
  77.   
  78.         System.out.println(formatDouble1(d));  
  79.         System.out.println(formatDouble2(d));  
  80.         System.out.println(formatDouble3(d));  
  81.         System.out.println(formatDouble4(d));  
  82.         System.out.println(formatDouble5(d));  
  83.     }  

对double d的值进行各种情况的赋值,如整数有值,整数为0,小数位数不够,小数四舍五入。经测试,前4种方法都会出现一些不精确的情况,只有最后种最精确。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值