zh_calc.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict';
  2. /**
  3. *
  4. * @author Mai
  5. * @date
  6. * @version
  7. */
  8. ;const zhBaseCalc = (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. Decimal.set({precision: 50, defaults: true});
  51. /**
  52. * 加法 num1 + num2
  53. * @param num1
  54. * @param num2
  55. * @returns {number}
  56. */
  57. function add(num1, num2) {
  58. return zhBaseCalc.add(num1 ? num1 : 0, num2 ? num2: 0);
  59. };
  60. /**
  61. * 减法 num1 - num2
  62. * @param num1
  63. * @param num2
  64. * @returns {number}
  65. */
  66. function sub(num1, num2) {
  67. return zhBaseCalc.sub(num1 ? num1 : 0, num2 ? num2 : 0);
  68. };
  69. /**
  70. * 乘法 num1 * num2
  71. * @param num1
  72. * @param num2
  73. * @returns {*}
  74. */
  75. function mul(num1, num2, digit = 6) {
  76. return Decimal.mul(num1 ? num1 : 0, num2 ? num2 : 0).toDecimalPlaces(digit).toNumber();
  77. };
  78. /**
  79. * 除法 num1 / num2
  80. * @param num1 - 被除数
  81. * @param num2 - 除数
  82. * @returns {*}
  83. */
  84. function div(num1, num2, digit = 6) {
  85. if (num2 && !checkZero(num2)) {
  86. return Decimal.div(num1 ? num1: 0, num2).toDecimalPlaces(digit).toNumber();
  87. } else {
  88. return null;
  89. }
  90. };
  91. /**
  92. * 四舍五入
  93. * @param {Number} value - 舍入的数字
  94. * @param {Number} decimal - 要保留的小数位数
  95. * @returns {*}
  96. */
  97. function round(value, decimal) {
  98. return value ? new Decimal(value).toDecimalPlaces(decimal).toNumber() : null;
  99. };
  100. return {add, sub, mul, div, round}
  101. })();