@@ -5,6 +5,10 @@ import { supportBigInt } from './supportUtil';
5
5
6
6
export type ValueType = string | number ;
7
7
8
+ function isEmpty ( value : ValueType ) {
9
+ return ( ! value && value !== 0 && ! Number . isNaN ( value ) ) || ! String ( value ) . trim ( ) ;
10
+ }
11
+
8
12
export interface DecimalClass {
9
13
add : ( value : ValueType ) => DecimalClass ;
10
14
@@ -38,7 +42,7 @@ export class NumberDecimal implements DecimalClass {
38
42
empty : boolean ;
39
43
40
44
constructor ( value : ValueType ) {
41
- if ( ( ! value && value !== 0 ) || ! String ( value ) . trim ( ) ) {
45
+ if ( isEmpty ( value ) ) {
42
46
this . empty = true ;
43
47
return ;
44
48
}
@@ -125,15 +129,15 @@ export class BigIntDecimal implements DecimalClass {
125
129
nan : boolean ;
126
130
127
131
constructor ( value : string | number ) {
128
- if ( ( ! value && value !== 0 ) || ! String ( value ) . trim ( ) ) {
132
+ if ( isEmpty ( value ) ) {
129
133
this . empty = true ;
130
134
return ;
131
135
}
132
136
133
137
this . origin = String ( value ) ;
134
138
135
139
// Act like Number convert
136
- if ( value === '-' ) {
140
+ if ( value === '-' || Number . isNaN ( value ) ) {
137
141
this . nan = true ;
138
142
return ;
139
143
}
@@ -265,32 +269,10 @@ export default function getMiniDecimal(value: ValueType): DecimalClass {
265
269
}
266
270
267
271
/**
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.
292
274
*/
293
- export function toFixed ( numStr : string , separatorStr : string , precision ?: number ) {
275
+ export function toFixed ( numStr : string , separatorStr : string , precision ?: number , cutOnly = false ) {
294
276
if ( numStr === '' ) {
295
277
return '' ;
296
278
}
@@ -303,11 +285,11 @@ export function toFixed(numStr: string, separatorStr: string, precision?: number
303
285
// We will get last + 1 number to check if need advanced number
304
286
const advancedNum = Number ( decimalStr [ precision ] ) ;
305
287
306
- if ( advancedNum >= 5 ) {
288
+ if ( advancedNum >= 5 && ! cutOnly ) {
307
289
const advancedDecimal = getMiniDecimal ( numStr ) . add (
308
290
`${ negativeStr } 0.${ '0' . repeat ( precision ) } ${ 10 - advancedNum } ` ,
309
291
) ;
310
- return toFixed ( advancedDecimal . toString ( ) , separatorStr , precision ) ;
292
+ return toFixed ( advancedDecimal . toString ( ) , separatorStr , precision , cutOnly ) ;
311
293
}
312
294
313
295
if ( precision === 0 ) {
0 commit comments