The solution of the number of decimal problems in JS decimal operation _javascript Skill

Source: Internet
Author: User
Tags mul pow

Written in front of the words:

Today to help colleagues solve a problem, is a decimal multiplication of many decimal places problem; This problem has been encountered before, and now deliberately to sum up;

Number type:

The number type is the most commonly used and most interesting type in ECMAScript; This type uses the IEEE754 format to represent integers and floating-point numbers (floating-point values are also doubles in some languages), and to support a variety of data types, ECMA-262 defines a different format for numerical surface quantities.

Decimal:
var intnum=10; Integer

Octal:
var octalnum1=070; Octal 56
var octalnum2=079; Invalid octal value-resolved to 79

The octal literal is not valid in strict mode;

Hexadecimal:
var hexnum1=0xa; 10
Remember that all values in eight and hexadecimal are eventually converted to decimal when the operation is performed;
Why is there an error in manipulating decimals?
The maximum progress of a floating-point number is 17 decimal digits, but it is not as accurate as an integer when the operation is performed, and the integer is converted to 10 when it is computed, and in Java and JavaScript, decimal decimal is converted to the corresponding binary. Part of the decimal is not fully converted into binary, here is the first error. After the decimal is converted to binary, then the binary operation, the binary results are obtained. The binary result is then converted to decimal, where a second error usually occurs.
So (0.1+0.2)!=03

Solution Method:

Program code
The Division function, which is used to get the exact division result
Description: JavaScript division results will be error, the two floating-point numbers are more obvious when dividing. This function returns a more precise division result.
Call: Accdiv (ARG1,ARG2)
Return value: Arg1 divided by Arg2 's exact result

function Accdiv (arg1,arg2) { 
var t1=0,t2=0,r1,r2; 
try{
t1=arg1.tostring (). Split (".") [1].length}catch (e) {
 }try{
 t2=arg2.tostring (). Split (".") [1].length}catch (e) {} 
 with (Math) { 
 r1=number (arg1.tostring (). Replace (".", "")) 
 R2=number ( Arg2.tostring (). Replace (".", "")) return 
(R1/R2) *pow (10,T2-T1);
}

Adding a Div method to the number type is more convenient to call.

Number.prototype.div = function (ARG) {return 
accdiv (this, ARG); 

multiplication function to get the exact result of the multiplication
Description: JavaScript multiplication results will be error, in two floating-point numbers are more obvious when multiplying. This function returns a more accurate result of the multiplication.
Call: Accmul (ARG1,ARG2)
return value: Arg1 times Arg2 's exact result

function Accmul (arg1,arg2) { 
var m=0,s1=arg1.tostring (),
s2=arg2.tostring (); 
try{
m+=s1.split (".") [1].length}catch (e) {} 
try{
m+=s2.split (".") [1].length}catch (e) {} return number 
(S1.replace (".", "")) *number (S2.replace (".", ""))/math.pow (10,m

Adding a Mul method to the number type is more convenient to call.

Number.prototype.mul = function (ARG) {return 
Accmul (ARG, this);
}

The addition function, which is used to get the exact addition result.
Description: JavaScript addition results will be error, in two floating-point number added when it is more obvious. This function returns a more precise addition result.
Call: Accadd (ARG1,ARG2)
return value: Arg1 plus arg2 's exact result

function Accadd (arg1,arg2) { 
var r1,r2,m; 
try{
r1=arg1.tostring (). Split (".") [1].length
}catch (e) {
r1=0} try{
r2=arg2.tostring (). Split (".") [1].length}catch (e) {r2=0} m=math.pow (10,math.max (R1,R2)) return 
(arg1*m+arg2*m)/M
}

Adding an Add method to the number type is more convenient to call.

Number.prototype.add = function (ARG) {return 
accadd (arg,this);
}

Include these functions in the place you want to use, and then call it to compute.
For example you want to calculate: 7*0.8, then change to (7). Mul (8)
Similar to other operations, you can get more accurate results.
Workaround Two:
The more commonly used approach, toFixed (), toFixed () method can be rounded to the specified number of decimal digits. It is OK to add this method after the result of our calculation, but it will have a slight effect on precision, if the precision is not high, it is recommended to use;

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.