fix(common): round currencies based on decimal digits in CurrencyPipe (#21783)

By default, we now round currencies based on the number of decimal digits available for that currency instead of using the rouding defined in the number formats.
More info about that can be found in http://www.unicode.org/cldr/charts/latest/supplemental/detailed_territory_currency_information.html#format_info

Fixes #10189

PR Close #21783
This commit is contained in:
Olivier Combe
2018-01-29 22:40:07 +01:00
committed by Miško Hevery
parent 0b2f7d13d0
commit 44154e71fd
9 changed files with 217 additions and 122 deletions

View File

@ -158,6 +158,7 @@ export default ${stringify(dayPeriodsSupplemental).replace(/undefined/g, '')};
*/
function generateBaseCurrencies(localeData, addDigits) {
const currenciesData = localeData.main('numbers/currencies');
const fractions = new cldrJs('en').get(`supplemental/currencyData/fractions`);
const currencies = {};
Object.keys(currenciesData).forEach(key => {
let symbolsArray = [];
@ -173,6 +174,16 @@ function generateBaseCurrencies(localeData, addDigits) {
symbolsArray = [, symbolNarrow];
}
}
if (addDigits && fractions[key] && fractions[key]['_digits']) {
const digits = parseInt(fractions[key]['_digits'], 10);
if (symbolsArray.length === 2) {
symbolsArray.push(digits);
} else if (symbolsArray.length === 1) {
symbolsArray = [...symbolsArray, , digits];
} else {
symbolsArray = [, , digits];
}
}
if (symbolsArray.length > 0) {
currencies[key] = symbolsArray;
}
@ -219,7 +230,7 @@ function generateCurrenciesFile() {
export type CurrenciesSymbols = [string] | [string | undefined, string];
/** @internal */
export const CURRENCIES_EN: {[code: string]: CurrenciesSymbols} = ${stringify(baseCurrencies, true)};
export const CURRENCIES_EN: {[code: string]: CurrenciesSymbols | [string | undefined, string | undefined, number]} = ${stringify(baseCurrencies, true)};
`;
}

View File

@ -132,6 +132,9 @@ export declare function getLocaleTimeFormat(locale: string, width: FormatWidth):
/** @experimental */
export declare function getLocaleWeekEndRange(locale: string): [WeekDay, WeekDay];
/** @experimental */
export declare function getNbOfCurrencyDigits(code: string): number;
/** @stable */
export declare class HashLocationStrategy extends LocationStrategy {
constructor(_platformLocation: PlatformLocation, _baseHref?: string);