@@ -14,6 +14,7 @@ const ZERO = "0";
1414const EMPTY = "" ;
1515
1616const literalRegExp = / ( \\ .) | ( [ ' ] [ ^ ' ] * [ ' ] ? ) | ( [ " ] [ ^ " ] * [ " ] ? ) / g;
17+ const trailingZerosRegExp = / ( \. (?: [ 0 - 9 ] * [ 1 - 9 ] ) ? ) 0 + $ / g;
1718const commaRegExp = / \, / g;
1819
1920function setFormatLiterals ( formatOptions ) {
@@ -31,6 +32,21 @@ function setFormatLiterals(formatOptions) {
3132 }
3233}
3334
35+ function trimTrailingZeros ( value , lastZero ) {
36+ let result ;
37+ if ( lastZero === 0 ) {
38+ result = value . replace ( trailingZerosRegExp , '$1' ) ;
39+ } else {
40+ result = value . replace ( new RegExp ( `(\\.[0-9]{${ lastZero } }[1-9]*)0+$` , 'g' ) , '$1' ) ;
41+ }
42+
43+ if ( result . charAt ( result . length - 1 ) === POINT ) {
44+ result = result . substr ( 0 , result . length - 1 ) ;
45+ }
46+
47+ return result ;
48+ }
49+
3450function roundNumber ( formatOptions ) {
3551 let { number, format } = formatOptions ;
3652 let decimalIndex = format . indexOf ( POINT ) ;
@@ -49,24 +65,30 @@ function roundNumber(formatOptions) {
4965 }
5066 fraction = fraction . split ( POINT ) [ 1 ] || EMPTY ;
5167
52- let idx = fraction . length ;
68+ let precision = fraction . length ;
69+ let trailingZeros = - 1 ;
5370
5471 if ( ! hasZero && ! hasSharp ) {
5572 formatOptions . format = format . substring ( 0 , decimalIndex ) + format . substring ( decimalIndex + 1 ) ;
5673 decimalIndex = - 1 ;
57- idx = 0 ;
74+ precision = 0 ;
5875 } else if ( hasZero && zeroIndex > sharpIndex ) {
59- idx = zeroIndex ;
76+ precision = zeroIndex ;
6077 } else if ( sharpIndex > zeroIndex ) {
61- if ( hasSharp && idx > sharpIndex ) {
62- idx = sharpIndex ;
63- } else if ( hasZero && idx < zeroIndex ) {
64- idx = zeroIndex ;
78+ if ( hasSharp && precision > sharpIndex ) {
79+ precision = sharpIndex ;
80+ } else if ( hasZero && precision < zeroIndex ) {
81+ precision = zeroIndex ;
6582 }
83+
84+ trailingZeros = hasZero ? zeroIndex : 0 ;
6685 }
6786
68- if ( idx > - 1 ) {
69- number = round ( number , idx ) ;
87+ if ( precision > - 1 ) {
88+ number = round ( number , precision ) ;
89+ if ( trailingZeros > - 1 ) {
90+ number = trimTrailingZeros ( number , trailingZeros ) ;
91+ }
7092 }
7193 } else {
7294 number = round ( number ) ;
0 commit comments