zh_calc.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. return num1 ? (num2 ? zhBaseCalc.add(num1, num2) : num1) : num2;
  60. };
  61. /**
  62. * 减法 num1 - num2
  63. * @param num1
  64. * @param num2
  65. * @returns {number}
  66. */
  67. function sub(num1, num2) {
  68. return zhBaseCalc.sub(num1 ? num1 : 0, num2 ? num2 : 0);
  69. };
  70. /**
  71. * 乘法 num1 * num2
  72. * @param num1
  73. * @param num2
  74. * @returns {*}
  75. */
  76. function mul(num1, num2, digit = 6) {
  77. //return Decimal.mul(num1 ? num1 : 0, num2 ? num2 : 0).toDecimalPlaces(digit).toNumber();
  78. return (num1 && num2) ? (Decimal.mul(num1, num2).toDecimalPlaces(digit).toNumber()) : 0;
  79. };
  80. /**
  81. * 除法 num1 / num2
  82. * @param num1 - 被除数
  83. * @param num2 - 除数
  84. * @returns {*}
  85. */
  86. function div(num1, num2, digit = 6) {
  87. if (num2 && !checkZero(num2)) {
  88. //return Decimal.div(num1 ? num1: 0, num2).toDecimalPlaces(digit).toNumber();
  89. return num1 ? (Decimal.div(num1, num2).toDecimalPlaces(digit).toNumber()) : 0;
  90. } else {
  91. return null;
  92. }
  93. };
  94. /**
  95. * 四舍五入
  96. * @param {Number} value - 舍入的数字
  97. * @param {Number} decimal - 要保留的小数位数
  98. * @returns {*}
  99. */
  100. function round(value, decimal) {
  101. return value ? new Decimal(value).toDecimalPlaces(decimal).toNumber() : null;
  102. };
  103. return {add, sub, mul, div, round}
  104. })();