6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { NumberFormatStyle , NumberSymbol , getLocaleNumberFormat , getLocaleNumberSymbol } from './locale_data_api' ;
9
+ import { NumberFormatStyle , NumberSymbol , getLocaleNumberFormat , getLocaleNumberSymbol , getNbOfCurrencyDigits } from './locale_data_api' ;
10
10
11
11
export const NUMBER_FORMAT_REGEXP = / ^ ( \d + ) ? \. ( ( \d + ) ( - ( \d + ) ) ? ) ? $ / ;
12
12
const MAX_DIGITS = 22 ;
@@ -36,21 +36,18 @@ function strToNumber(value: number | string): number {
36
36
* Transforms a number to a locale string based on a style and a format
37
37
*/
38
38
function formatNumber (
39
- value : number | string , locale : string , style : NumberFormatStyle , groupSymbol : NumberSymbol ,
40
- decimalSymbol : NumberSymbol , digitsInfo ?: string ) : string {
41
- const format = getLocaleNumberFormat ( locale , style ) ;
42
- const num = strToNumber ( value ) ;
43
-
44
- const pattern = parseNumberFormat ( format , getLocaleNumberSymbol ( locale , NumberSymbol . MinusSign ) ) ;
39
+ value : number | string , pattern : ParsedNumberFormat , locale : string , groupSymbol : NumberSymbol ,
40
+ decimalSymbol : NumberSymbol , digitsInfo ?: string , isPercent = false ) : string {
45
41
let formattedText = '' ;
46
42
let isZero = false ;
43
+ const num = strToNumber ( value ) ;
47
44
48
45
if ( ! isFinite ( num ) ) {
49
46
formattedText = getLocaleNumberSymbol ( locale , NumberSymbol . Infinity ) ;
50
47
} else {
51
48
let parsedNumber = parseNumber ( num ) ;
52
49
53
- if ( style === NumberFormatStyle . Percent ) {
50
+ if ( isPercent ) {
54
51
parsedNumber = toPercent ( parsedNumber ) ;
55
52
}
56
53
@@ -142,13 +139,20 @@ function formatNumber(
142
139
143
140
/**
144
141
* Formats a currency to a locale string
142
+ *
143
+ * @internal
145
144
*/
146
145
export function formatCurrency (
147
146
value : number | string , locale : string , currency : string , currencyCode ?: string ,
148
147
digitsInfo ?: string ) : string {
148
+ const format = getLocaleNumberFormat ( locale , NumberFormatStyle . Currency ) ;
149
+ const pattern = parseNumberFormat ( format , getLocaleNumberSymbol ( locale , NumberSymbol . MinusSign ) ) ;
150
+
151
+ pattern . minFrac = getNbOfCurrencyDigits ( currencyCode ! ) ;
152
+ pattern . maxFrac = pattern . minFrac ;
153
+
149
154
const res = formatNumber (
150
- value , locale , NumberFormatStyle . Currency , NumberSymbol . CurrencyGroup ,
151
- NumberSymbol . CurrencyDecimal , digitsInfo ) ;
155
+ value , pattern , locale , NumberSymbol . CurrencyGroup , NumberSymbol . CurrencyDecimal , digitsInfo ) ;
152
156
return res
153
157
. replace ( CURRENCY_CHAR , currency )
154
158
// if we have 2 time the currency character, the second one is ignored
@@ -157,22 +161,27 @@ export function formatCurrency(
157
161
158
162
/**
159
163
* Formats a percentage to a locale string
164
+ *
165
+ * @internal
160
166
*/
161
167
export function formatPercent ( value : number | string , locale : string , digitsInfo ?: string ) : string {
168
+ const format = getLocaleNumberFormat ( locale , NumberFormatStyle . Percent ) ;
169
+ const pattern = parseNumberFormat ( format , getLocaleNumberSymbol ( locale , NumberSymbol . MinusSign ) ) ;
162
170
const res = formatNumber (
163
- value , locale , NumberFormatStyle . Percent , NumberSymbol . Group , NumberSymbol . Decimal ,
164
- digitsInfo ) ;
171
+ value , pattern , locale , NumberSymbol . Group , NumberSymbol . Decimal , digitsInfo , true ) ;
165
172
return res . replace (
166
173
new RegExp ( PERCENT_CHAR , 'g' ) , getLocaleNumberSymbol ( locale , NumberSymbol . PercentSign ) ) ;
167
174
}
168
175
169
176
/**
170
177
* Formats a number to a locale string
178
+ *
179
+ * @internal
171
180
*/
172
181
export function formatDecimal ( value : number | string , locale : string , digitsInfo ?: string ) : string {
173
- return formatNumber (
174
- value , locale , NumberFormatStyle . Decimal , NumberSymbol . Group , NumberSymbol . Decimal ,
175
- digitsInfo ) ;
182
+ const format = getLocaleNumberFormat ( locale , NumberFormatStyle . Decimal ) ;
183
+ const pattern = parseNumberFormat ( format , getLocaleNumberSymbol ( locale , NumberSymbol . MinusSign ) ) ;
184
+ return formatNumber ( value , pattern , locale , NumberSymbol . Group , NumberSymbol . Decimal , digitsInfo ) ;
176
185
}
177
186
178
187
interface ParsedNumberFormat {
0 commit comments