JavaScript Precision Subtraction

Source: Internet
Author: User
Tags mul pow

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
  1. addition function
  2. function Accadd (arg1, arg2) {
  3. var r1, R2, M;
  4. try {
  5. R1 = Arg1.tostring (). Split (".")  [1].length;
  6. }
  7. catch (e) {
  8. R1 = 0;
  9. }
  10. try {
  11. r2 = arg2.tostring (). Split (".")  [1].length;
  12. }
  13. catch (e) {
  14. r2 = 0;
  15. }
  16. m = Math.pow (Ten, Math.max (R1, r2));
  17. return (ARG1 * m + arg2 * m)/m;
  18. }
  19. Add an Add method to the number type, which is used directly with. Add to complete the calculation.
  20. Number.prototype.add = function (ARG) {
  21. return Accadd (ARG, this );
  22. };
  23. Subtraction function
  24. function Subtr (arg1, arg2) {
  25. var r1, R2, M, N;
  26. try {
  27. R1 = Arg1.tostring (). Split (".")  [1].length;
  28. }
  29. catch (e) {
  30. R1 = 0;
  31. }
  32. try {
  33. r2 = arg2.tostring (). Split (".")  [1].length;
  34. }
  35. catch (e) {
  36. r2 = 0;
  37. }
  38. m = Math.pow (Ten, Math.max (R1, r2));
  39. //last Modify by Deeka
  40. //Dynamic control accuracy length
  41. n = (r1 >= r2)? R1:R2;
  42. return ((ARG1 * M-ARG2 * m)/m). ToFixed (n);
  43. }
  44. Add an Add method to the number type, and use the. Sub directly to complete the calculation.
  45. Number.prototype.sub = function (ARG) {
  46. return SUBTR (this, ARG);
  47. };
  48. multiplication function
  49. function Accmul (arg1, arg2) {
  50. var m = 0, S1 = arg1.tostring (), S2 = arg2.tostring ();
  51. try {
  52. M + = S1.split (".")  [1].length;
  53. }
  54. catch (e) {
  55. }
  56. try {
  57. M + = S2.split (".")  [1].length;
  58. }
  59. catch (e) {
  60. }
  61. return Number (S1.replace (".", "")) * Number (S2.replace (".", "")) /Math.pow (ten, M);
  62. }
  63. Add a Mul method to the number type and use it directly with the. Mul to complete the calculation.
  64. Number.prototype.mul = function (ARG) {
  65. return Accmul (ARG, this );
  66. };
  67. Division function
  68. function Accdiv (arg1, arg2) {
  69. var t1 = 0, t2 = 0, R1, R2;
  70. try {
  71. T1 = arg1.tostring (). Split (".")  [1].length;
  72. }
  73. catch (e) {
  74. }
  75. try {
  76. T2 = arg2.tostring (). Split (".")  [1].length;
  77. }
  78. catch (e) {
  79. }
  80. With (Math) {
  81. R1 = number (arg1.tostring (). Replace (".", ""));
  82. r2 = number (arg2.tostring (). Replace (".", ""));
  83. return (R1/R2) * POW (T2-T1);
  84. }
  85. }
  86. Add a div method to the number type, and use the. Div directly to complete the calculation.
  87. Number.prototype.div = function (ARG) {
  88. return Accdiv (this, ARG);
  89. };
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
  1. Examples of additions (others are similar)
  2. function Calculate () {
  3. //Number 1
  4. var num1 = 10;
  5. //Number 2
  6. var num2 = 5;
  7. //Calculate NUM1 + num2
  8. Alert (Num1.add (num2));
  9. }
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

Related Article

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.