zh_calc.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. /**
  3. *
  4. * @author Mai
  5. * @date
  6. * @version
  7. */
  8. ;const zhBaseCalc = (function () {
  9. const zeroPrecision = 12, 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. function isNonZero(num) {
  44. return num && round(num, zeroPrecision) !== 0;
  45. }
  46. return {
  47. digitLength: digitLength,
  48. powLength: powLength,
  49. round: round,
  50. add: add, sub: sub, mul: mul, div: div,
  51. isNonZero: isNonZero,
  52. }
  53. })();
  54. /**
  55. * 计算(四则、舍入) 统一,方便以后置换
  56. * @type {{add, sub, mul, div, round}}
  57. */
  58. const ZhCalc = (function () {
  59. Decimal.set({precision: 50, defaults: true});
  60. /**
  61. * 加法 num1 + num2
  62. * @param num1
  63. * @param num2
  64. * @returns {number}
  65. */
  66. function add(num1, num2) {
  67. //return zhBaseCalc.add(num1 ? num1 : 0, num2 ? num2: 0);
  68. return num1 ? (num2 ? zhBaseCalc.add(num1, num2) : num1) : num2;
  69. };
  70. /**
  71. * 减法 num1 - num2
  72. * @param num1
  73. * @param num2
  74. * @returns {number}
  75. */
  76. function sub(num1, num2) {
  77. return zhBaseCalc.sub(num1 ? num1 : 0, num2 ? num2 : 0);
  78. }
  79. /**
  80. * 乘法 num1 * num2
  81. * @param num1
  82. * @param num2
  83. * @returns {*}
  84. */
  85. function mul(num1, num2, digit = 6) {
  86. //return Decimal.mul(num1 ? num1 : 0, num2 ? num2 : 0).toDecimalPlaces(digit).toNumber();
  87. return (num1 && num2) ? (Decimal.mul(num1, num2).toDecimalPlaces(digit).toNumber()) : 0;
  88. }
  89. /**
  90. * 除法 num1 / num2
  91. * @param num1 - 被除数
  92. * @param num2 - 除数
  93. * @returns {*}
  94. */
  95. function div(num1, num2, digit = 6) {
  96. if (num2 && !checkZero(num2)) {
  97. //return Decimal.div(num1 ? num1: 0, num2).toDecimalPlaces(digit).toNumber();
  98. return num1 ? (Decimal.div(num1, num2).toDecimalPlaces(digit).toNumber()) : 0;
  99. } else {
  100. return null;
  101. }
  102. }
  103. /**
  104. * 四舍五入
  105. * @param {Number} value - 舍入的数字
  106. * @param {Number} decimal - 要保留的小数位数
  107. * @returns {*}
  108. */
  109. function round(value, decimal) {
  110. decimal = decimal ? parseInt(decimal) : 0;
  111. return value ? new Decimal(value).toDecimalPlaces(decimal).toNumber() : null;
  112. }
  113. return {add, sub, mul, div, round, isNonZero: zhBaseCalc.isNonZero}
  114. })();