@@ -48,11 +48,7 @@ export default function NumberFormat(config = options) {
4848 if ( this . options . reverseFill ) {
4949 this . number = toFixed ( this . numberOnly ( this . input , / \D + / g) , this . options . precision ) . replace ( '.' , this . options . decimal )
5050 } 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 )
5652 } else {
5753 this . number = this . numberOnly ( this . input , new RegExp ( `[^0-9\\${ this . options . decimal } ]+` , 'gi' ) )
5854 this . number = this . parts ( this . number ) . join ( this . options . decimal )
@@ -71,23 +67,30 @@ export default function NumberFormat(config = options) {
7167 this . parts = ( number = '' , decimal = this . options . decimal ) => {
7268 var parts = number . toString ( ) . split ( decimal )
7369 parts [ 0 ] = this . toNumber ( parts [ 0 ] ) || 0
70+
7471 if ( parts . length > 1 ) {
7572 parts [ 1 ] = parts . slice ( 1 , parts . length ) . join ( '' )
7673 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 ( '.' )
7985 }
8086 }
87+
8188 return parts . slice ( 0 , 2 )
8289 }
8390
8491 this . addSeparator = ( ) => {
8592 var parts = this . numbers ( ) . split ( this . options . decimal )
8693 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- }
9194 return parts . join ( this . options . decimal )
9295 }
9396
0 commit comments