Java processing of small numbers: high-precision operations with BigDecimal class, precision retention method, that is, the designation of the Rounding method

Source: Internet
Author: User

First, the computer's fractional calculation is accurate within a certain range, exceeding the range can only approximate value:
    计算机存储的浮点数受存储bit位数影响,只能保证一定范围内精准,超过bit范围的只能取近似值。    java中各类型的精度范围参见:http://blog.csdn.net/longshenlmj/article/details/47616481
Note When programming:
doulbe类型的数,不能用等号判定是否相等(或者是一定范围内可以)。因为两次同样的计算(除法)结果可能出现小数部分不同。甚至极端的时候,初始化两个小数时,都可能不相等(用数值和字符串分别初始化bigdecimal的小数就会不等)
The experience of the Java Decimal processing method is summarized as follows:
(1)小数计算对精度无要求时,使用float节省时间。(2)如果有精度要求,用BigDecimal类处理(初始化必须使用字符串,因为用数值初始化会得到近似值,不准确),然后设置保留位数和 舍入法(half_up四舍五入,half_even银行家,half_down向下取整)(3)精度要求低时可转化为整数处理(集体统一扩大数量级):    乘以10的级数转化为整数处理,小数点右移几位,但整数值不要超过对应类型的取值范围。比如保留4位小数,可统一乘以10000,然后只保留整数计算结果,保留近位的话就多乘一位。    这种方式在RTB项目MDSP的算法核心模块中使用,几十万的投放量,用int或long就可以处理,更大范围的整数处理BigInteger。

The benefits are:

a,计算快,除了除法,其他运算上整数计算(加减乘)节省时间;b,除法中,小数部分可直接省略,或向上取整(小数大于0时则加1)也可以让小数点多移动一位来保留进位。
Second, the Java specialized fractional operation class: BigDecimal type (more accurate decimal calculation than double and float decimal)
    float和double只能用来做科学计算或者是工程计算,在广域数值范围上提供较为精确的快速近似计算;而在商业计算要求结果精确(比如,有的编程语言中提供了专门的货币类型来处理),所以Java使用java.math.BigDecimal专门处理小数精度
Constructor Description:
    BigDecimal(int)       创建一个具有参数所指定整数值的对象。     BigDecimal(double)    创建一个具有参数所指定双精度值的对象。     BigDecimal(long)      创建一个具有参数所指定长整数值的对象。     BigDecimal(String)    创建一个具有参数所指定以字符串表示的数值的对象。

usage principle: Initializing decimals must be made with a string, BigDecimal (string), because decimals with double are approximate, not exact values.

BigDecimal member Methods
    add(BigDecimal)        对象自身与参数相加,然后返回这个对象。     subtract(BigDecimal)   对象自身与参数相减,然后返回这个对象。     multiply(BigDecimal)   对象自身与参数相乘,然后返回这个对象。     divide(BigDecimal)     对象自身与参数相除,然后返回这个对象。     toString()             BigDecimal对象的数值转换成对应的字符串。     doubleValue()          BigDecimal对返回double值。     floatValue()           BigDecimal对返回float。     longValue()            BigDecimal对返回long值。     intValue()             BigDecimal对返回int值。
Third, the method of rounding of the Java decimal reserved precision 1,java commonly used rounding method to realize:
Math类中的round方法不能设置保留几位小数,但可以乘100达到保留2位的目的:        Math.round(value*100)/100.0;或者,直接用java.text.DecimalFormat指定保留几位小数,用哪几种舍入法:        DecimalFormat decFormat = new DecimalFormat("#.00");        decFormat.setRoundingMode(RoundingMode.HALF_UP);
8 Ways to round the 2,java:
1、 ROUND_UP:向上取整(丢掉小数,整数加1) 远离零方向舍入。向绝对值最大的方向舍入,只要舍弃位非0即进位。2、ROUND_DOWN:向下取整(丢掉小数)。趋向零方向舍入。向绝对值最小的方向输入,所有的位都要舍弃,不存在进位情况。3、ROUND_CEILING:向正无穷方向走,始终不会减少计算值。如果 BigDecimal 为正,则舍入行为与 ROUND_UP 相同;如果为负,则舍入行为与 ROUND_DOWN 相同。Math.round()方法就是使用的此模式。4、ROUND_FLOOR:向负无穷方向舍入。向负无穷方向靠拢。若是正数,舍入行为类似于ROUND_DOWN;若为负数,舍入行为类似于ROUND_UP。5、 HALF_UP:四舍五入,最近数字舍入(5进)。6、 HALF_DOWN:四舍六入,最近数字舍入(5舍)。7、 HAIL_EVEN:银行家舍入法。四舍六入五偶舍。即舍弃位4舍6入,当为5时看前一位,奇进偶舍。向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。     也就是说,如果舍弃部分左边的数字为奇数,则舍入行为与 ROUND_HALF_UP 相同; 如果为偶数,则舍入行为与 ROUND_HALF_DOWN 相同。     注意,在重复进行一系列计算时,此舍入模式可以将累加错误减到最小。8、ROUND_UNNECESSARY 断言请求的操作具有精确的结果,因此不需要舍入。如果对获得精确结果的操作指定此舍入模式,则抛出ArithmeticException。
Iv. Java code examples for BigDecimal
 PackageTestImportJava.math.BigDecimal; Public  class bigdecimaltest {     Public Static void Main(string[] args) {BigDecimal A =NewBigDecimal (1); BigDecimal B =NewBigDecimal (3); BigDecimal sum =NewBigDecimal ("0"); for(intI=0;i<2; i++) {sum = Sum.add (a); }//Division operationSystem.out.println ("a/b="+a.divide (b,5, bigdecimal.round_half_up). Doublevalue ()); System.out.println ("a/b="+a.divide (b,6, bigdecimal.round_half_up). Multiply (NewBigDecimal (" the"))); System.out.println ("a/b="+a.divide (b,6, bigdecimal.round_half_up). Multiply (NewBigDecimal (" the"). Doublevalue ());//System.out.println ("a/b=" +a.divide (b,11). Doublevalue ());//Do not specify rounding method, error "Invalid rounding mode"        intmytt= -;//integers can be arbitrarily initializedBigDecimal Oirginc =NewBigDecimal (integer.tostring (MYTT)); BigDecimal oirgin2 =NewBigDecimal (MYTT); System.out.println ("Oirginc:"+OIRGINC); System.out.println ("oirgin2:"+OIRGIN2); BigDecimal scale =NewBigDecimal ("0.002");//decimals must be in string, otherwise there will be an error, such as Double        Doubletestfloat=0.001; BigDecimal Scale2 =NewBigDecimal (TestFloat);        SYSTEM.OUT.PRINTLN (scale); System.out.println (Scale2);//Output for approximate number 0.001000000000000000020816681711721685132943093776702880859375System.out.println (Scale.equals (scale2));///Two comparison objects must be on the same scale to be unequal with equal,2.0 and 2.00, so use compareSystem.out.println (Scale.compareto (scale2));//output, less than, equal to, greater than the corresponding-1, 0, 1. Can be used with different scale scales of the object, as long as the value is equal to the line, such as 1.0 equals 1.00        if(0= = B.compareto (NewBigDecimal ("3.00"))){//number of different scalesSystem.out.println ("Equal"); }//bigdecimal multiplication operation returns int typeSystem.out.println ("multipy:"+oirginc.multiply (scale). Intvalue ());//bigdecimal addition operation returns int typeSystem.out.println ("Add:"+b.add (a). Doublevalue ());//bigdecimal subtraction operation returns int typeSystem.out.println ("Substract:"+b.subtract (NewBigDecimal (3.00). Doublevalue ());//math.ceil () rounding up, that is, the decimal is not 0 integers plus 1 decimal        intTest1 =3;DoubleTest2=0.01;        System.out.println (Double.valueof (Math.ceil (test1 * test2)). Intvalue ()); System.out.println (Math.ceil (1.02)); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java processing of small numbers: high-precision operations with BigDecimal class, precision retention method, that is, the designation of the Rounding method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.