@@ -5,6 +5,10 @@ import { supportBigInt } from './supportUtil';
55
66export type ValueType = string | number ;
77
8+ function isEmpty ( value : ValueType ) {
9+ return ( ! value && value !== 0 && ! Number . isNaN ( value ) ) || ! String ( value ) . trim ( ) ;
10+ }
11+
812export interface DecimalClass {
913 add : ( value : ValueType ) => DecimalClass ;
1014
@@ -38,7 +42,7 @@ export class NumberDecimal implements DecimalClass {
3842 empty : boolean ;
3943
4044 constructor ( value : ValueType ) {
41- if ( ( ! value && value !== 0 ) || ! String ( value ) . trim ( ) ) {
45+ if ( isEmpty ( value ) ) {
4246 this . empty = true ;
4347 return ;
4448 }
@@ -125,15 +129,15 @@ export class BigIntDecimal implements DecimalClass {
125129 nan : boolean ;
126130
127131 constructor ( value : string | number ) {
128- if ( ( ! value && value !== 0 ) || ! String ( value ) . trim ( ) ) {
132+ if ( isEmpty ( value ) ) {
129133 this . empty = true ;
130134 return ;
131135 }
132136
133137 this . origin = String ( value ) ;
134138
135139 // Act like Number convert
136- if ( value === '-' ) {
140+ if ( value === '-' || Number . isNaN ( value ) ) {
137141 this . nan = true ;
138142 return ;
139143 }
@@ -265,32 +269,10 @@ export default function getMiniDecimal(value: ValueType): DecimalClass {
265269}
266270
267271/**
268- * round up an unsigned number str, like: 1.4 -> 2, 1.5 -> 2
269- */
270- export function roundUpUnsignedDecimal ( numStr : string , precision : number ) {
271- const { integerStr, decimalStr } = trimNumber ( numStr ) ;
272- const advancedDecimal = getMiniDecimal ( integerStr + '.' + decimalStr ) . add (
273- `0.${ '0' . repeat ( precision ) } ${ 5 } ` ,
274- ) ;
275- return toFixed ( advancedDecimal . toString ( ) , '.' , precision ) ;
276- }
277-
278- /**
279- * round up an unsigned number str, like: 1.4 -> 1, 1.5 -> 1
280- */
281- export function roundDownUnsignedDecimal ( numStr : string , precision : number ) {
282- const { negativeStr, integerStr, decimalStr } = trimNumber ( numStr ) ;
283- const numberWithoutDecimal = `${ negativeStr } ${ integerStr } ` ;
284- if ( precision === 0 ) {
285- return integerStr ;
286- }
287- return `${ numberWithoutDecimal } .${ decimalStr . padEnd ( precision , '0' ) . slice ( 0 , precision ) } ` ;
288- }
289-
290- /**
291- * Align the logic of toFixed to around like 1.5 => 2
272+ * Align the logic of toFixed to around like 1.5 => 2.
273+ * If set `cutOnly`, will just remove the over decimal part.
292274 */
293- export function toFixed ( numStr : string , separatorStr : string , precision ?: number ) {
275+ export function toFixed ( numStr : string , separatorStr : string , precision ?: number , cutOnly = false ) {
294276 if ( numStr === '' ) {
295277 return '' ;
296278 }
@@ -303,11 +285,11 @@ export function toFixed(numStr: string, separatorStr: string, precision?: number
303285 // We will get last + 1 number to check if need advanced number
304286 const advancedNum = Number ( decimalStr [ precision ] ) ;
305287
306- if ( advancedNum >= 5 ) {
288+ if ( advancedNum >= 5 && ! cutOnly ) {
307289 const advancedDecimal = getMiniDecimal ( numStr ) . add (
308290 `${ negativeStr } 0.${ '0' . repeat ( precision ) } ${ 10 - advancedNum } ` ,
309291 ) ;
310- return toFixed ( advancedDecimal . toString ( ) , separatorStr , precision ) ;
292+ return toFixed ( advancedDecimal . toString ( ) , separatorStr , precision , cutOnly ) ;
311293 }
312294
313295 if ( precision === 0 ) {
0 commit comments