In a recent project to use JS to realize the function of automatic calculation, this thought just to achieve simple addition, subtraction, multiplication, except on it, so rinsed finished.
Just when I was happy, I found the problem ...
When doing some floating-point arithmetic, the calculation results are let me surprise ah, that value let me laugh and cry, a long list of values, too cow ...
I was wondering!! But fortunately a lot of cattle, gave me a solution, hey ... The problem basically solved, in order to express the feeling, I decided to put the code out, we share, hope to give some people some help.
JS Code
- addition function
- 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 (Ten, Math.max (R1, r2));
- return (ARG1 * m + arg2 * m)/m;
- }
- Add an Add method to the number type, which is used directly with. Add to complete the calculation.
- Number.prototype.add = function (ARG) {
- return Accadd (ARG, this );
- };
- Subtraction function
- function Subtr (arg1, arg2) {
- var r1, R2, M, N;
- 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 (Ten, Math.max (R1, r2));
- //last Modify by Deeka
- //Dynamic control accuracy length
- n = (r1 >= r2)? R1:R2;
- return ((ARG1 * M-ARG2 * m)/m). ToFixed (n);
- }
- Add an Add method to the number type, and use the. Sub directly to complete the calculation.
- Number.prototype.sub = function (ARG) {
- return SUBTR (this, ARG);
- };
- multiplication function
- 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 (ten, M);
- }
- Add a Mul method to the number type and use it directly with the. Mul to complete the calculation.
- Number.prototype.mul = function (ARG) {
- return Accmul (ARG, this );
- };
- Division function
- 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 (T2-T1);
- }
- }
- Add a div method to the number type, and use the. Div directly to complete the calculation.
- Number.prototype.div = function (ARG) {
- return Accdiv (this, ARG);
- };
Addition functions 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 (Ten, Math.max (R1, r2)); return (ARG1 * m + arg2 * m)/m;} Add an Add method to the number type, which is used directly with. Add to complete the calculation. Number.prototype.add = function (ARG) {return Accadd (ARG, this);};/ /subtraction Functions function Subtr (arg1, arg2) {var r1, R2, M, n;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 (Ten, Math.max (R1, r2)); Last modify by Deeka//dynamic control accuracy length n = (r1 >= r2)? R1:r2;return ((arg1 * M-ARG2 * m)/m). ToFixed (n);} Add an Add method to the number type, and use the. Sub directly to complete the calculation. Number.prototype.sub = function (ARG) {return SUBTR (this, arg);};/ /multiplication Functions 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 (ten, M);} Add a Mul method to the number type and use it directly with the. Mul to complete the calculation. Number.prototype.mul = function (ARG) {return Accmul (ARG, this);}; The Division functions 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 (ten, T2-t1);}} Add a div method to the number type, and use the. Div directly to complete the calculation. Number.prototype.div = function (ARG) {return Accdiv (this, arg);};
How to use:
JS Code
- Examples of additions (others are similar)
- function Calculate () {
- //Number 1
- var num1 = 10;
- //Number 2
- var num2 = 5;
- //Calculate NUM1 + num2
- Alert (Num1.add (num2));
- }
The addition example (others are similar) function calculate () {//number 1 var num1 = ten; Number 2 var num2 = 5; Calculate NUM1 + num2 alert (Num1.add (num2));}
JavaScript Precision Subtraction