count.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //加法
  2. function addNum(arg1, arg2) {
  3. arg1 = arg1 ? arg1 : 0
  4. arg2 = arg2 ? arg2 : 0
  5. let r1, r2, m, c;
  6. try {
  7. r1 = arg1.toString().split(".")[1].length;
  8. } catch (e) {
  9. r1 = 0;
  10. }
  11. try {
  12. r2 = arg2.toString().split(".")[1].length;
  13. } catch (e) {
  14. r2 = 0;
  15. }
  16. c = Math.abs(r1 - r2);
  17. m = Math.pow(10, Math.max(r1, r2));
  18. if (c > 0) {
  19. let cm = Math.pow(10, c);
  20. if (r1 > r2) {
  21. arg1 = Number(arg1.toString().replace(".", ""));
  22. arg2 = Number(arg2.toString().replace(".", "")) * cm;
  23. } else {
  24. arg1 = Number(arg1.toString().replace(".", "")) * cm;
  25. arg2 = Number(arg2.toString().replace(".", ""));
  26. }
  27. } else {
  28. arg1 = Number(arg1.toString().replace(".", ""));
  29. arg2 = Number(arg2.toString().replace(".", ""));
  30. }
  31. return (arg1 + arg2) / m;
  32. }
  33. //减法
  34. function subtraction(num1, num2) {
  35. var r1, r2, m, floatNum;
  36. try {
  37. r1 = num1.toString().split(".")[1].length;
  38. } catch (e) {
  39. r1 = 0;
  40. }
  41. try {
  42. r2 = num2.toString().split(".")[1].length;
  43. } catch (e) {
  44. r2 = 0;
  45. }
  46. m = Math.pow(10, Math.max(r1, r2));
  47. return (num1 * m - num2 * m) / m;
  48. }
  49. //相乘
  50. function multiply(arg1, arg2) {
  51. arg1 = arg1 ? arg1 : 0
  52. arg2 = arg2 ? arg2 : 0
  53. let r1, r2, m, c;
  54. try {
  55. r1 = arg1.toString().split(".")[1].length;
  56. } catch (e) {
  57. r1 = 0;
  58. }
  59. try {
  60. r2 = arg2.toString().split(".")[1].length;
  61. } catch (e) {
  62. r2 = 0;
  63. }
  64. c = Math.abs(r1 - r2);
  65. m = Math.pow(10, Math.max(r1, r2) * 2);
  66. if (c > 0) {
  67. let cm = Math.pow(10, c);
  68. if (r1 > r2) {
  69. arg1 = Number(arg1.toString().replace(".", ""));
  70. arg2 = Number(arg2.toString().replace(".", "")) * cm;
  71. } else {
  72. arg1 = Number(arg1.toString().replace(".", "")) * cm;
  73. arg2 = Number(arg2.toString().replace(".", ""));
  74. }
  75. } else {
  76. arg1 = Number(arg1.toString().replace(".", ""));
  77. arg2 = Number(arg2.toString().replace(".", ""));
  78. }
  79. return (arg1 * arg2) / m;
  80. }
  81. module.exports = {
  82. addNum,
  83. subtraction,
  84. multiply
  85. };