The problem of multiple digits in the multiplication or dividing of JS decimal point

Source: Internet
Author: User
Tags pow

A recent project to make a payment requires a calculator, so a problem has been found.

Like what:

0.03/0.00003=999.9999999999999

0.0003*0.3=0.000029999999999999997

0.1+0.2=0.30000000000000004

etc...

look at the back. The reasons are:

The maximum progress of a floating-point value is 17 decimal places, but it is far less accurate than an integer when it is being computed, and integers are converted to 10 when the arithmetic is performed, while in Java and JavaScript the decimal decimal is converted to the corresponding binary. Some decimals are not fully converted into binary, where the first error occurs. After the decimal is converted to binary, the binary operation is performed, and the binary result is obtained. The binary results are then converted to decimal, where the second error is usually present.

So (0.1+0.2)!=03

Paste the previous method directly:

function ToDecimal2 (X,SS) {var f = parsefloat (x);       if (IsNaN (f)) {return false;       } var f = math.round (x*100)/100;       var s = f.tostring ();       var rs = s.indexof ('. ');         if (Rs < 0) {rs = s.length;       s + = '. ';       } while (s.length <= RS + ss) {s + = ' 0 ';     } return s; } function bcfixed (num, s) {var times = Math.pow (ten, s) var des = num * times + 0.5 des = parseint (DES, ten)/Ti Mes return ToDecimal2 (des,s) + ";} /** * Addition operation, to avoid the data added after the decimal point to produce multiple digits and the loss of computational accuracy. * * @param num1 addend 1 | Num2 addend 2 */function bcadd (NUM1, num2,s) {var basenum, baseNum1, Basenum2,ret; try {baseNum1 = num1.tostring (). Split ("." ) [1].length; } catch (e) {baseNum1 = 0;} try {baseNum2 = num2.tostring (). Split (".") [1].length; } catch (e) {baseNum2 = 0;} basenum = Math.pow (Ten, Math.max (BASENUM1, baseNum2)); ret= (NUM1 * basenum + num2 * basenum) /Basenum; Return bcfixed (ret,s);};/ * * Addition operation to avoid the loss of multi-digit and computational accuracy when the data is reduced by a few points. * * @paramNUM1 Minuend | num2 meiosis */function bcsub (NUM1, num2,s) {var basenum, baseNum1, Basenum2,ret; var precision;//precision try {baseNum1 = num1.to String (). Split (".") [1].length; } catch (e) {baseNum1 = 0;} try {baseNum2 = num2.tostring (). Split (".") [1].length; } catch (e) {baseNum2 = 0;} basenum = Math.pow (Math.max (BASENUM1, baseNum2)); Precision = (baseNum1 >= baseNum2)? Basenum1:basenum2;ret= ((NUM1 * basenum-num2 * basenum)/basenum). toFixed (precision); Return bcfixed (ret,s);};/ * * multiplication to avoid data multiplication after the decimal point resulting in multiple digits and loss of computational accuracy. * * @param num1 by multiplier | Num2 multiplier */function Bcmul (NUM1, num2,s) {var basenum = 0,ret; try {basenum + = Num1.tostring (). Split (".") [1].length; } catch (e) {} try {basenum + = Num2.tostring (). Split (".") [1].length; } catch (e) {} ret=number (Num1.tostring (). Replace (".", "")) * Number (num2.tostring (). Replace (".", ""))/Math.pow (BAS ENum); Return bcfixed (ret,s);};/ * * Division operation, to avoid the multiplication of data after the decimal point and the loss of computational precision. * * @param NUM1 Dividend | NUM2 divisor */function bcdiv (NUM1, num2,s) {var baseNum1 =0, baseNum2 = 0,ret; var baseNum3, BaseNum4; try {baseNum1 = num1.tostring (). Split (".") [1].length; } catch (e) {baseNum1 = 0;} try {baseNum2 = num2.tostring (). Split (".") [1].length;  } catch (e) {baseNum2 = 0;} with (Math) {baseNum3 = number (num1.tostring (). Replace (".", ""));  BASENUM4 = number (num2.tostring (). Replace (".", "")); ret= (BASENUM3/BASENUM4) * POW (ten, basenum2-basenum1); Return bcfixed (ret,s); }};

  

Call Method: (The following S is displayed after the decimal number, such as S is 0 o'clock display integer, 2 to show two decimal places, such as 2.00)

Addition: Bcadd (NUM1, num2,s)//such as: 1+2 for Bcadd (1, 2,2)

Subtraction: Bcsub (NUM1, num2,s)//such as: 1-2 for Bcsub (1, 2,2)

Multiplication: Bcmul (NUM1, num2,s)//such as: 1*2 for Bcmul (1, 2,2)

Division: Bcdiv (NUM1, num2,s)//such as: 1/2 for Bcdiv (1, 2,s)

The problem of multiple digits in the multiplication or dividing of JS decimal point

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.