Skip to content

Commit 28110d1

Browse files
authored
Currency formatter (ServiceNowDevProgram#869)
* Create currencyFormatting.js 2 examples how can you format currencies for different locales and different formats * Create README.md description how you can customize the format of your output
1 parent ed4cb4a commit 28110d1

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Format currency values to your preferred locale and format.
2+
Examples are provided for EUR, HUF with English and Hungarian number format.
3+
4+
formatString values:
5+
Valid values:
6+
7+
%s: Replaced by the currency symbol associated with the country code specified in the format() call.
8+
%v: Replaced by the currency amount, such as 123.45.
9+
%c: Replaced by the ISO currency code specified in the format() call, such as USD or EUR.
10+
%l: Replaced with the passed in value, no formatting performed.
11+
%p: Replaced by the percent sign (%).
12+
For example, if the format string is '%s%v%c' and the value to format is 123.45 in US dollars, the returned formatted string is $123.45 USD. If the format string is '%s%l%c' and the value string to format is '56M' in Euros, the returned formatted string is €56M EUR.
13+
14+
setMaxFractionDigits:
15+
Maximum number of fraction digits to return. Does a rounding based on the closest right-most digit.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var amount = '9123456.123456789';
2+
var currencyCode = 'EUR';
3+
var formatString = '%s%v %c';
4+
var exchangeValue = new sn_currency.GlideCurrencyFormatter(formatString);
5+
exchangeValue.setLocale("en", "EN"); // Language = en Country = EN
6+
7+
gs.info('Formatted currency: ' + exchangeValue.setMaxFractionDigits(1).format(amount, currencyCode));
8+
// expected output: Formatted currency: €9,123,456.1 EUR
9+
10+
amount = '9123456.127456789';
11+
currencyCode = 'HUF';
12+
formatString = '%v %c';
13+
exchangeValue = new sn_currency.GlideCurrencyFormatter(formatString);
14+
exchangeValue.setLocale("hu", "HU"); // Language = hu Country = HU
15+
16+
gs.info('Formatted currency: ' + exchangeValue.setMaxFractionDigits(2).format(amount, currencyCode));
17+
// expected output: Formatted currency: 9 123 456,13 HUF

0 commit comments

Comments
 (0)