ll-package
Version:
2 lines (1 loc) • 3.32 kB
JavaScript
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const n=require("./numberUtil.js"),S=require("./supportUtil.js");function d(i){return!i&&i!==0&&!Number.isNaN(i)||!String(i).trim()}class u{constructor(t){if(this.origin="",d(t)){this.empty=!0;return}this.origin=String(t),this.number=Number(t)}negate(){return new u(-this.toNumber())}add(t){if(this.isInvalidate())return new u(t);const e=Number(t);if(Number.isNaN(e))return this;const r=this.number+e;if(r>Number.MAX_SAFE_INTEGER)return new u(Number.MAX_SAFE_INTEGER);if(r<Number.MIN_SAFE_INTEGER)return new u(Number.MIN_SAFE_INTEGER);const s=Math.max(n.getNumberPrecision(this.number),n.getNumberPrecision(e));return new u(r.toFixed(s))}isEmpty(){return this.empty}isNaN(){return Number.isNaN(this.number)}isInvalidate(){return this.isEmpty()||this.isNaN()}equals(t){return this.toNumber()===(t==null?void 0:t.toNumber())}lessEquals(t){return this.add(t.negate().toString()).toNumber()<=0}toNumber(){return this.number}toString(){return(arguments.length>0&&arguments[0]!==void 0?arguments[0]:!0)?this.isInvalidate()?"":n.num2str(this.number):this.origin}}class a{constructor(t){if(this.origin="",d(t)){this.empty=!0;return}if(this.origin=String(t),t==="-"||Number.isNaN(t)){this.nan=!0;return}let e=t;if(n.isE(e)&&(e=Number(e)),e=typeof e=="string"?e:n.num2str(e),n.validateNumber(e)){const r=n.trimNumber(e);this.negative=r.negative;const s=r.trimStr.split(".");this.integer=BigInt(s[0]);const m=s[1]||"0";this.decimal=BigInt(m),this.decimalLen=m.length}else this.nan=!0}getMark(){return this.negative?"-":""}getIntegerStr(){return this.integer.toString()}getDecimalStr(){return this.decimal.toString().padStart(this.decimalLen,"0")}alignDecimal(t){const e=`${this.getMark()}${this.getIntegerStr()}${this.getDecimalStr().padEnd(t,"0")}`;return BigInt(e)}negate(){const t=new a(this.toString());return t.negative=!t.negative,t}add(t){if(this.isInvalidate())return new a(t);const e=new a(t);if(e.isInvalidate())return this;const r=Math.max(this.getDecimalStr().length,e.getDecimalStr().length),s=this.alignDecimal(r),m=e.alignDecimal(r),o=(s+m).toString(),{negativeStr:l,trimStr:g}=n.trimNumber(o),h=`${l}${g.padStart(r+1,"0")}`;return new a(`${h.slice(0,-r)}.${h.slice(-r)}`)}isEmpty(){return this.empty}isNaN(){return this.nan}isInvalidate(){return this.isEmpty()||this.isNaN()}equals(t){return this.toString()===(t==null?void 0:t.toString())}lessEquals(t){return this.add(t.negate().toString()).toNumber()<=0}toNumber(){return this.isNaN()?NaN:Number(this.toString())}toString(){return(arguments.length>0&&arguments[0]!==void 0?arguments[0]:!0)?this.isInvalidate()?"":n.trimNumber(`${this.getMark()}${this.getIntegerStr()}.${this.getDecimalStr()}`).fullStr:this.origin}}function c(i){return S.supportBigInt()?new a(i):new u(i)}function N(i,t,e){let r=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!1;if(i==="")return"";const{negativeStr:s,integerStr:m,decimalStr:o}=n.trimNumber(i),l=`${t}${o}`,g=`${s}${m}`;if(e>=0){const h=Number(o[e]);if(h>=5&&!r){const f=c(i).add(`${s}0.${"0".repeat(e)}${10-h}`);return N(f.toString(),t,e,r)}return e===0?g:`${g}${t}${o.padEnd(e,"0").slice(0,e)}`}return l===".0"?g:`${g}${l}`}exports.BigIntDecimal=a;exports.NumberDecimal=u;exports.default=c;exports.toFixed=N;