6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { NumberFormatStyle , NumberSymbol , getLocaleNumberFormat , getLocaleNumberSymbol , getNbOfCurrencyDigits } from './locale_data_api' ;
9
+ import { NumberFormatStyle , NumberSymbol , getLocaleNumberFormat , getLocaleNumberSymbol , getNumberOfCurrencyDigits } from './locale_data_api' ;
10
10
11
11
export const NUMBER_FORMAT_REGEXP = / ^ ( \d + ) ? \. ( ( \d + ) ( - ( \d + ) ) ? ) ? $ / ;
12
12
const MAX_DIGITS = 22 ;
@@ -18,34 +18,19 @@ const DIGIT_CHAR = '#';
18
18
const CURRENCY_CHAR = '¤' ;
19
19
const PERCENT_CHAR = '%' ;
20
20
21
- /**
22
- * Transforms a string into a number (if needed)
23
- */
24
- function strToNumber ( value : number | string ) : number {
25
- // Convert strings to numbers
26
- if ( typeof value === 'string' && ! isNaN ( + value - parseFloat ( value ) ) ) {
27
- return + value ;
28
- }
29
- if ( typeof value !== 'number' ) {
30
- throw new Error ( `${ value } is not a number` ) ;
31
- }
32
- return value ;
33
- }
34
-
35
21
/**
36
22
* Transforms a number to a locale string based on a style and a format
37
23
*/
38
- function formatNumber (
39
- value : number | string , pattern : ParsedNumberFormat , locale : string , groupSymbol : NumberSymbol ,
24
+ function formatNumberToLocaleString (
25
+ value : number , pattern : ParsedNumberFormat , locale : string , groupSymbol : NumberSymbol ,
40
26
decimalSymbol : NumberSymbol , digitsInfo ?: string , isPercent = false ) : string {
41
27
let formattedText = '' ;
42
28
let isZero = false ;
43
- const num = strToNumber ( value ) ;
44
29
45
- if ( ! isFinite ( num ) ) {
30
+ if ( ! isFinite ( value ) ) {
46
31
formattedText = getLocaleNumberSymbol ( locale , NumberSymbol . Infinity ) ;
47
32
} else {
48
- let parsedNumber = parseNumber ( num ) ;
33
+ let parsedNumber = parseNumber ( value ) ;
49
34
50
35
if ( isPercent ) {
51
36
parsedNumber = toPercent ( parsedNumber ) ;
@@ -128,7 +113,7 @@ function formatNumber(
128
113
}
129
114
}
130
115
131
- if ( num < 0 && ! isZero ) {
116
+ if ( value < 0 && ! isZero ) {
132
117
formattedText = pattern . negPre + formattedText + pattern . negSuf ;
133
118
} else {
134
119
formattedText = pattern . posPre + formattedText + pattern . posSuf ;
@@ -138,20 +123,32 @@ function formatNumber(
138
123
}
139
124
140
125
/**
141
- * Formats a currency to a locale string
126
+ * @ngModule CommonModule
127
+ * @whatItDoes Formats a number as currency using locale rules.
128
+ * @description
142
129
*
143
- * @internal
130
+ * Use `currency` to format a number as currency.
131
+ *
132
+ * Where:
133
+ * - `value` is a number.
134
+ * - `locale` is a `string` defining the locale to use.
135
+ * - `currency` is the string that represents the currency, it can be its symbol or its name.
136
+ * - `currencyCode` is the [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) currency code, such
137
+ * as `USD` for the US dollar and `EUR` for the euro.
138
+ * - `digitInfo` See {@link DecimalPipe} for more details.
139
+ *
140
+ * @stable
144
141
*/
145
142
export function formatCurrency (
146
- value : number | string , locale : string , currency : string , currencyCode ?: string ,
143
+ value : number , locale : string , currency : string , currencyCode ?: string ,
147
144
digitsInfo ?: string ) : string {
148
145
const format = getLocaleNumberFormat ( locale , NumberFormatStyle . Currency ) ;
149
146
const pattern = parseNumberFormat ( format , getLocaleNumberSymbol ( locale , NumberSymbol . MinusSign ) ) ;
150
147
151
- pattern . minFrac = getNbOfCurrencyDigits ( currencyCode ! ) ;
148
+ pattern . minFrac = getNumberOfCurrencyDigits ( currencyCode ! ) ;
152
149
pattern . maxFrac = pattern . minFrac ;
153
150
154
- const res = formatNumber (
151
+ const res = formatNumberToLocaleString (
155
152
value , pattern , locale , NumberSymbol . CurrencyGroup , NumberSymbol . CurrencyDecimal , digitsInfo ) ;
156
153
return res
157
154
. replace ( CURRENCY_CHAR , currency )
@@ -160,28 +157,48 @@ export function formatCurrency(
160
157
}
161
158
162
159
/**
163
- * Formats a percentage to a locale string
160
+ * @ngModule CommonModule
161
+ * @whatItDoes Formats a number as a percentage according to locale rules.
162
+ * @description
163
+ *
164
+ * Formats a number as percentage.
164
165
*
165
- * @internal
166
+ * Where:
167
+ * - `value` is a number.
168
+ * - `locale` is a `string` defining the locale to use.
169
+ * - `digitInfo` See {@link DecimalPipe} for more details.
170
+ *
171
+ * @stable
166
172
*/
167
- export function formatPercent ( value : number | string , locale : string , digitsInfo ?: string ) : string {
173
+ export function formatPercent ( value : number , locale : string , digitsInfo ?: string ) : string {
168
174
const format = getLocaleNumberFormat ( locale , NumberFormatStyle . Percent ) ;
169
175
const pattern = parseNumberFormat ( format , getLocaleNumberSymbol ( locale , NumberSymbol . MinusSign ) ) ;
170
- const res = formatNumber (
176
+ const res = formatNumberToLocaleString (
171
177
value , pattern , locale , NumberSymbol . Group , NumberSymbol . Decimal , digitsInfo , true ) ;
172
178
return res . replace (
173
179
new RegExp ( PERCENT_CHAR , 'g' ) , getLocaleNumberSymbol ( locale , NumberSymbol . PercentSign ) ) ;
174
180
}
175
181
176
182
/**
177
- * Formats a number to a locale string
183
+ * @ngModule CommonModule
184
+ * @whatItDoes Formats a number according to locale rules.
185
+ * @description
186
+ *
187
+ * Formats a number as text. Group sizing and separator and other locale-specific
188
+ * configurations are based on the locale.
189
+ *
190
+ * Where:
191
+ * - `value` is a number.
192
+ * - `locale` is a `string` defining the locale to use.
193
+ * - `digitInfo` See {@link DecimalPipe} for more details.
178
194
*
179
- * @internal
195
+ * @stable
180
196
*/
181
- export function formatDecimal ( value : number | string , locale : string , digitsInfo ?: string ) : string {
197
+ export function formatNumber ( value : number , locale : string , digitsInfo ?: string ) : string {
182
198
const format = getLocaleNumberFormat ( locale , NumberFormatStyle . Decimal ) ;
183
199
const pattern = parseNumberFormat ( format , getLocaleNumberSymbol ( locale , NumberSymbol . MinusSign ) ) ;
184
- return formatNumber ( value , pattern , locale , NumberSymbol . Group , NumberSymbol . Decimal , digitsInfo ) ;
200
+ return formatNumberToLocaleString (
201
+ value , pattern , locale , NumberSymbol . Group , NumberSymbol . Decimal , digitsInfo ) ;
185
202
}
186
203
187
204
interface ParsedNumberFormat {
@@ -335,7 +352,7 @@ function parseNumber(num: number): ParsedNumber {
335
352
digits = [ ] ;
336
353
// Convert string to array of digits without leading/trailing zeros.
337
354
for ( j = 0 ; i <= zeros ; i ++ , j ++ ) {
338
- digits [ j ] = + numStr . charAt ( i ) ;
355
+ digits [ j ] = Number ( numStr . charAt ( i ) ) ;
339
356
}
340
357
}
341
358
@@ -424,7 +441,6 @@ function roundNumber(parsedNumber: ParsedNumber, minFrac: number, maxFrac: numbe
424
441
}
425
442
}
426
443
427
- /** @internal */
428
444
export function parseIntAutoRadix ( text : string ) : number {
429
445
const result : number = parseInt ( text ) ;
430
446
if ( isNaN ( result ) ) {
0 commit comments