@@ -48,11 +48,7 @@ export default function NumberFormat(config = options) {
48
48
if ( this . options . reverseFill ) {
49
49
this . number = toFixed ( this . numberOnly ( this . input , / \D + / g) , this . options . precision ) . replace ( '.' , this . options . decimal )
50
50
} else if ( typeof this . input === 'number' ) {
51
- if ( this . isClean ) {
52
- this . number = this . toNumber ( this . input . toFixed ( this . options . precision ) ) . toString ( ) . replace ( '-' , '' ) . replace ( '.' , this . options . decimal )
53
- } else {
54
- this . number = this . toNumber ( this . input ) . toString ( ) . replace ( '-' , '' ) . replace ( '.' , this . options . decimal )
55
- }
51
+ this . number = this . parts ( this . input . toString ( ) . replace ( '-' , '' ) , '.' ) . join ( this . options . decimal )
56
52
} else {
57
53
this . number = this . numberOnly ( this . input , new RegExp ( `[^0-9\\${ this . options . decimal } ]+` , 'gi' ) )
58
54
this . number = this . parts ( this . number ) . join ( this . options . decimal )
@@ -71,23 +67,30 @@ export default function NumberFormat(config = options) {
71
67
this . parts = ( number = '' , decimal = this . options . decimal ) => {
72
68
var parts = number . toString ( ) . split ( decimal )
73
69
parts [ 0 ] = this . toNumber ( parts [ 0 ] ) || 0
70
+
74
71
if ( parts . length > 1 ) {
75
72
parts [ 1 ] = parts . slice ( 1 , parts . length ) . join ( '' )
76
73
parts = parts . slice ( 0 , 2 )
77
- if ( this . isClean && parts [ 1 ] . length ) {
78
- parts [ 1 ] = this . toNumber ( `.${ parts [ 1 ] } ` ) . toFixed ( this . options . precision ) . toString ( ) . replace ( '0.' , '' )
74
+ }
75
+
76
+ if ( this . isClean ) {
77
+ const newNumber = this . toNumber ( parts . join ( '.' ) ) . toFixed ( this . options . precision )
78
+ const cleanNumber = this . toNumber ( newNumber )
79
+ const minimumDigits = cleanNumber . toFixed ( this . options . minimumFractionDigits )
80
+
81
+ if ( this . options . minimumFractionDigits && this . options . minimumFractionDigits >= 0 && cleanNumber . toString ( ) . length < minimumDigits . length ) {
82
+ parts = minimumDigits . toString ( ) . split ( '.' )
83
+ } else {
84
+ parts = cleanNumber . toString ( ) . split ( '.' )
79
85
}
80
86
}
87
+
81
88
return parts . slice ( 0 , 2 )
82
89
}
83
90
84
91
this . addSeparator = ( ) => {
85
92
var parts = this . numbers ( ) . split ( this . options . decimal )
86
93
parts [ 0 ] = parts [ 0 ] . toString ( ) . replace ( / ( \d ) (? = (?: \d { 3 } ) + \b ) / gm, `$1${ this . options . separator } ` )
87
- if ( this . isClean ) {
88
- parts [ 1 ] = this . toNumber ( `.${ parts [ 1 ] } ` ) . toString ( ) . replace ( '0.' , '' )
89
- return parts [ 1 ] && parts [ 1 ] > 0 ? parts . join ( this . options . decimal ) : parts [ 0 ]
90
- }
91
94
return parts . join ( this . options . decimal )
92
95
}
93
96
0 commit comments