BigIntDecimal.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
  2. import _createClass from "@babel/runtime/helpers/esm/createClass";
  3. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  4. import { isE, isEmpty, num2str, trimNumber, validateNumber } from "./numberUtil";
  5. var BigIntDecimal = /*#__PURE__*/function () {
  6. /** BigInt will convert `0009` to `9`. We need record the len of decimal */
  7. function BigIntDecimal(value) {
  8. _classCallCheck(this, BigIntDecimal);
  9. _defineProperty(this, "origin", '');
  10. _defineProperty(this, "negative", void 0);
  11. _defineProperty(this, "integer", void 0);
  12. _defineProperty(this, "decimal", void 0);
  13. _defineProperty(this, "decimalLen", void 0);
  14. _defineProperty(this, "empty", void 0);
  15. _defineProperty(this, "nan", void 0);
  16. if (isEmpty(value)) {
  17. this.empty = true;
  18. return;
  19. }
  20. this.origin = String(value);
  21. // Act like Number convert
  22. if (value === '-' || Number.isNaN(value)) {
  23. this.nan = true;
  24. return;
  25. }
  26. var mergedValue = value;
  27. // We need convert back to Number since it require `toFixed` to handle this
  28. if (isE(mergedValue)) {
  29. mergedValue = Number(mergedValue);
  30. }
  31. mergedValue = typeof mergedValue === 'string' ? mergedValue : num2str(mergedValue);
  32. if (validateNumber(mergedValue)) {
  33. var trimRet = trimNumber(mergedValue);
  34. this.negative = trimRet.negative;
  35. var numbers = trimRet.trimStr.split('.');
  36. this.integer = BigInt(numbers[0]);
  37. var decimalStr = numbers[1] || '0';
  38. this.decimal = BigInt(decimalStr);
  39. this.decimalLen = decimalStr.length;
  40. } else {
  41. this.nan = true;
  42. }
  43. }
  44. _createClass(BigIntDecimal, [{
  45. key: "getMark",
  46. value: function getMark() {
  47. return this.negative ? '-' : '';
  48. }
  49. }, {
  50. key: "getIntegerStr",
  51. value: function getIntegerStr() {
  52. return this.integer.toString();
  53. }
  54. /**
  55. * @private get decimal string
  56. */
  57. }, {
  58. key: "getDecimalStr",
  59. value: function getDecimalStr() {
  60. return this.decimal.toString().padStart(this.decimalLen, '0');
  61. }
  62. /**
  63. * @private Align BigIntDecimal with same decimal length. e.g. 12.3 + 5 = 1230000
  64. * This is used for add function only.
  65. */
  66. }, {
  67. key: "alignDecimal",
  68. value: function alignDecimal(decimalLength) {
  69. var str = "".concat(this.getMark()).concat(this.getIntegerStr()).concat(this.getDecimalStr().padEnd(decimalLength, '0'));
  70. return BigInt(str);
  71. }
  72. }, {
  73. key: "negate",
  74. value: function negate() {
  75. var clone = new BigIntDecimal(this.toString());
  76. clone.negative = !clone.negative;
  77. return clone;
  78. }
  79. }, {
  80. key: "cal",
  81. value: function cal(offset, calculator, calDecimalLen) {
  82. var maxDecimalLength = Math.max(this.getDecimalStr().length, offset.getDecimalStr().length);
  83. var myAlignedDecimal = this.alignDecimal(maxDecimalLength);
  84. var offsetAlignedDecimal = offset.alignDecimal(maxDecimalLength);
  85. var valueStr = calculator(myAlignedDecimal, offsetAlignedDecimal).toString();
  86. var nextDecimalLength = calDecimalLen(maxDecimalLength);
  87. // We need fill string length back to `maxDecimalLength` to avoid parser failed
  88. var _trimNumber = trimNumber(valueStr),
  89. negativeStr = _trimNumber.negativeStr,
  90. trimStr = _trimNumber.trimStr;
  91. var hydrateValueStr = "".concat(negativeStr).concat(trimStr.padStart(nextDecimalLength + 1, '0'));
  92. return new BigIntDecimal("".concat(hydrateValueStr.slice(0, -nextDecimalLength), ".").concat(hydrateValueStr.slice(-nextDecimalLength)));
  93. }
  94. }, {
  95. key: "add",
  96. value: function add(value) {
  97. if (this.isInvalidate()) {
  98. return new BigIntDecimal(value);
  99. }
  100. var offset = new BigIntDecimal(value);
  101. if (offset.isInvalidate()) {
  102. return this;
  103. }
  104. return this.cal(offset, function (num1, num2) {
  105. return num1 + num2;
  106. }, function (len) {
  107. return len;
  108. });
  109. }
  110. }, {
  111. key: "multi",
  112. value: function multi(value) {
  113. var target = new BigIntDecimal(value);
  114. if (this.isInvalidate() || target.isInvalidate()) {
  115. return new BigIntDecimal(NaN);
  116. }
  117. return this.cal(target, function (num1, num2) {
  118. return num1 * num2;
  119. }, function (len) {
  120. return len * 2;
  121. });
  122. }
  123. }, {
  124. key: "isEmpty",
  125. value: function isEmpty() {
  126. return this.empty;
  127. }
  128. }, {
  129. key: "isNaN",
  130. value: function isNaN() {
  131. return this.nan;
  132. }
  133. }, {
  134. key: "isInvalidate",
  135. value: function isInvalidate() {
  136. return this.isEmpty() || this.isNaN();
  137. }
  138. }, {
  139. key: "equals",
  140. value: function equals(target) {
  141. return this.toString() === (target === null || target === void 0 ? void 0 : target.toString());
  142. }
  143. }, {
  144. key: "lessEquals",
  145. value: function lessEquals(target) {
  146. return this.add(target.negate().toString()).toNumber() <= 0;
  147. }
  148. }, {
  149. key: "toNumber",
  150. value: function toNumber() {
  151. if (this.isNaN()) {
  152. return NaN;
  153. }
  154. return Number(this.toString());
  155. }
  156. }, {
  157. key: "toString",
  158. value: function toString() {
  159. var safe = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  160. if (!safe) {
  161. return this.origin;
  162. }
  163. if (this.isInvalidate()) {
  164. return '';
  165. }
  166. return trimNumber("".concat(this.getMark()).concat(this.getIntegerStr(), ".").concat(this.getDecimalStr())).fullStr;
  167. }
  168. }]);
  169. return BigIntDecimal;
  170. }();
  171. export { BigIntDecimal as default };