zh_calc.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. /**
  3. *
  4. * @author Mai
  5. * @date
  6. * @version
  7. */
  8. ;const baseCalc = (function () {
  9. const mulPrecision = 12, divPrecision = 12;
  10. function digitLength (num) {
  11. // 兼容科学计数
  12. var eSplit = num.toString().split(/[eE]/);
  13. var len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0));
  14. return len > 0 ? len : 0;
  15. }
  16. function powLength (num) {
  17. var rs = num.toString();
  18. if (rs.indexOf('+') > 0) {
  19. return rs.match(/0*$/g).length();
  20. } else {
  21. const eSplit = rs.split(/[eE]/);
  22. const len = Number(eSplit[1]) - this.digitLength(eSplit[0]);
  23. return len > 0 ? len : 0;
  24. }
  25. }
  26. function round (num, digit) {
  27. return Math.round(num * Math.pow(10, digit)) / Math.pow(10, digit);
  28. }
  29. function add(num1, num2) {
  30. var d1 = this.digitLength(num1), d2 = this.digitLength(num2);
  31. return this.round(num1 + num2, Math.max(d1, d2));
  32. }
  33. function sub(num1, num2) {
  34. var d1 = this.digitLength(num1), d2 = this.digitLength(num2);
  35. return this.round(num1 - num2, Math.max(d1, d2));
  36. }
  37. function mul(num1, num2) {
  38. return this.round(num1 * num2, mulPrecision);
  39. }
  40. function div(num1, num2) {
  41. return this.round(num1 / num2, divPrecision);
  42. }
  43. return { digitLength: digitLength, powLength: powLength, round: round, add: add, sub: sub, mul: mul, div: div }
  44. })();
  45. /**
  46. * 计算(四则、舍入) 统一,方便以后置换
  47. * @type {{add, sub, mul, div, round}}
  48. */
  49. const ZhCalc = (function () {
  50. /**
  51. * 加法 num1 + num2
  52. * @param num1
  53. * @param num2
  54. * @returns {number}
  55. */
  56. function add(num1, num2) {
  57. return baseCalc.add(num1 ? num1 : 0, num2 ? num2: 0);
  58. };
  59. /**
  60. * 减法 num1 - num2
  61. * @param num1
  62. * @param num2
  63. * @returns {number}
  64. */
  65. function sub(num1, num2) {
  66. return baseCalc.sub(num1 ? num1 : 0, num2 ? num2 : 0);
  67. };
  68. /**
  69. * 乘法 num1 * num2
  70. * @param num1
  71. * @param num2
  72. * @returns {*}
  73. */
  74. function mul(num1, num2, digit = 6) {
  75. return Decimal.mul(num1 ? num1 : 0, num2 ? num2 : 0).toDecimalPlaces(digit).toNumber();
  76. };
  77. /**
  78. * 除法 num1 / num2
  79. * @param num1 - 被除数
  80. * @param num2 - 除数
  81. * @returns {*}
  82. */
  83. function div(num1, num2, digit = 6) {
  84. if (num2 && !checkZero(num2)) {
  85. return Decimal.div(num1 ? num1: 0, num2).toDecimalPlaces(digit).toNumber();
  86. } else {
  87. return null;
  88. }
  89. };
  90. /**
  91. * 四舍五入
  92. * @param {Number} value - 舍入的数字
  93. * @param {Number} decimal - 要保留的小数位数
  94. * @returns {*}
  95. */
  96. function round(value, decimal) {
  97. return value ? Decimal.round(value, decimal).toNumber() : null;
  98. };
  99. return {add, sub, mul, div, round}
  100. })();