Skip to content

Commit

Permalink
Doc: Add currency formatting
Browse files Browse the repository at this point in the history
- Add `.currencyFormatter( currency [, options] )`;
- Add `.formatCurrency( value, currency [, options] )`;

Ref #238
  • Loading branch information
rxaviers committed Dec 2, 2014
1 parent 428c291 commit a6d3246
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 2 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Node.js module.
- [Date module](#date_module)
- [Message module](#message_module)
- [Number module](#number_module)
- [Currency module](#currency_module)
- [Plural module](#plural_module)
- more to come...
- [Error reference](#error)
Expand Down Expand Up @@ -97,9 +98,10 @@ information on its usage.
| File | Minified + gzipped size | Summary |
|---|--:|---|
| globalize.js | 1.1KB | [Core library](#core) |
| globalize/currency.js | +2.6KB | [Currency module](#currency_module) provides currency formatting and parsing |
| globalize/date.js | +3.8KB | [Date module](#date_module) provides date formatting and parsing |
| globalize/message.js | +0.5KB | [Message module](#message_module) provides message translation |
| globalize/number.js | +2.6KB | [Number module](#number_module) provides number formatting and parsing |
| globalize/number.js | +2.7KB | [Number module](#number_module) provides number formatting and parsing |
| globalize/plural.js | +1.7KB | [Plural module](#plural_module) provides pluralization support |
<!--- By updating this table, also update its clone in #usage -->

Expand Down Expand Up @@ -167,6 +169,7 @@ requirements. See table below.
| Module | Required CLDR JSON files |
|---|---|
| Core module | cldr/supplemental/likelySubtags.json |
| Currency module | cldr/main/`locale`/currencies.json<br>cldr/supplemental/currencyData.json |
| Date module | cldr/main/`locale`/ca-gregorian.json<br>cldr/main/`locale`/timeZoneNames.json<br>cldr/supplemental/timeData.json<br>cldr/supplemental/weekData.json |
| Number module | cldr/main/`locale`/numbers.json |
| Plural module | cldr/supplemental/plurals.json |
Expand Down Expand Up @@ -311,6 +314,22 @@ to you in different flavors):

[Read more...](doc/api/number/parse-number.md)

<a name="currency_module"></a>
#### Currency module

- **`.currencyFormatter( currency [, options] )`**

Return a function that formats a currency according to the given options or
locale's defaults.

[Read more...](doc/api/number/currency-formatter.md)

- **`.formatCurrency( value, currency [, options] )`**

Format a currency according to the given options or locale's defaults.

[Read more...](doc/api/number/format-currency.md)

<a name="plural_module"></a>
### Plural module

Expand Down Expand Up @@ -380,6 +399,11 @@ to you in different flavors):
setting the Global locale with `Globalize.locale( <locale> )`.
[Read more...](doc/error/e-default-locale-not-defined.md)

- **`E_MISSING_PLURAL_MODULE`**

Thrown when plural module is needed, but not loaded, eg. to format currencies
using the named form. [Read more...](doc/error/e-missing-plural-module.md)

- **`E_UNSUPPORTED`**

Thrown for unsupported features, eg. to format unsupported date patterns.
Expand Down
140 changes: 140 additions & 0 deletions doc/api/currency/currency-formatter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
## .currencyFormatter( currency [, options] )

Return a function that formats a currency according to the given options or
locale's defaults.

### Parameters

**currency**

3-letter currency code as defined by ISO 4217, eg. `USD`.

**options** Optional

A JSON object including none or any of the following options.

> **style** Optional
>
> String `decimal` (default), or `percent`.
>
> **minimumIntegerDigits** Optional
>
> Non-negative integer Number value indicating the minimum integer digits to be
> used. Numbers will be padded with leading zeroes if necessary.
>
> **minimumFractionDigits** and **maximumFractionDigits** Optional
>
> Non-negative integer Number values indicating the minimum and maximum fraction
> digits to be used. Numbers will be rounded or padded with trailing zeroes if
> necessary. Either one or both of these properties must be present. If they
> are, they will override minimum and maximum fraction digits derived from the
> CLDR patterns.
>
> **minimumSignificantDigits** and **maximumSignificantDigits** Optional
>
> Positive integer Number values indicating the minimum and maximum fraction
> digits to be shown. Either none or both of these properties are present. If
> they are, they override minimum and maximum integer and fraction digits. The
> formatter uses however many integer and fraction digits are required to
> display the specified number of significant digits.
>
> **round** Optional
>
> String with rounding method `ceil`, `floor`, `round` (default), or `truncate`.
>
> **useGrouping** Optional
>
> Boolean (default is true) value indicating whether a grouping separator should
> be used.
### Example

You can use the static method `Globalize.currencyFormatter()`, which uses the
default locale.

```javascript
var formatter;

Globalize.locale( "en" );
formatter = Globalize.currencyFormatter( "USD" );

formatter( 9.99 ); // "$9.99"
```

You can use the instance method `.currencyFormatter()`, which uses the instance
locale.

```javascript
var deFormatter = Globalize( "de" ).currencyFormatter( "EUR" ),
zhFormatter = Globalize( "zh" ).currencyFormatter( "EUR" );

deFormatter( 9.99 ); // "9,99 €"
zhFormatter( 9.99 ); // "€ 9.99"
```

For comparison, follow the formatting output of different symbols in different
locales.

| 3-letter currency code | en (English) | de (German) | zh (Chinese) |
| `.currencyFormatter( "USD" )( 1 )` | `$1.00` | `1,00 $` | `US$ 1.00` |
| `.currencyFormatter( "EUR" )( 1 )` | `€1.00` | `1,00 €` | `€ 1.00` |
| `.currencyFormatter( "CNY" )( 1 )` | `CN¥1.00` | `1,00 CN¥` | `¥ 1.00` |
| `.currencyFormatter( "JPY" )( 1 )` | `¥1` | `1 ¥` | `JP¥ 1` |
| `.currencyFormatter( "GBP" )( 1 )` | `£1.00` | `1,00 £` | `£ 1.00` |
| `.currencyFormatter( "BRL" )( 1 )` | `R$1.00` | `1,00 R$` | `R$ 1.00` |

For plural messages, use `style: "name"`.

```javascript
var formatter;

Globalize.locale( "en" );
formatter = Globalize.currencyFormatter( "USD", { style: "name" } );

formatter( 0 ); // "0.00 US dollars"
formatter( 1 ); // "1.00 US dollar"
```

For comparison, follow the formatting output of different symbols in different
locales using the plural messages `Globalize( locale ).currencyFormatter( currency,
{ style: "name" } )( 1 )`.

| `USD` | `1.00 US dollar` | `1,00 US-Dollar` | `1.00美元` |
| `EUR` | `1.00 euro` | `1,00 Euro` | `1.00欧元` |
| `CNY` | `1.00 Chinese yuan` | `1,00 Chinesischer Yuan` | `1.00人民币` |
| `JPY` | `1 Japanese yen` | `1 Japanischer Yen` | `1日元` |
| `GBP` | `1.00 British pound sterling` | `1,00 Britisches Pfund Sterling` | `1.00英镑` |
| `BRL` | `1.00 Brazilian real` | `1,00 Brasilianischer Real` | `1.00巴西雷亚尔` |

For the international currency code, use `style: "code"`.

```javascript
var formatter;

Globalize.locale( "en" );
formatter = Globalize.currencyFormatter( "USD", { style: "code" } );

formatter( 9.99 );
// "9.99 USD"
```

Override the number of digits, grouping separators, rounding function or any
other [`.numberFormatter()` options](../number/number-formatter.md).

```javascript
var formatter;

Globalize.locale( "en" );
formatter = Globalize.currencyFormatter( "USD", {
minimumFractionDigits: 0,
style: "name"
});

formatter( 1 ); // "1 US dollar"

formatter = Globalize.currencyFormatter( "USD", {
round: "ceil"
});

formatter( 1.491 ); // "$1.50"
```
100 changes: 100 additions & 0 deletions doc/api/currency/format-currency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
## .formatCurrency( value, currency [, options] )

Format a currency ( `value`, `currency` ) according to the given `options`.

*Important*: Use [`.currencyFormatter( currency [, options]
)`](./currency-formatter.md) instead when formatting more then one number, for
improved performance.

### Parameters

**value**

Number to be formatted, eg. `9.99`.

**currency**

3-letter currency code as defined by ISO 4217, eg. `USD`.

**options** Optional

See [`.currencyFormatter( [options] )`](./currency-formatter.md).

### Example

You can use the static method `Globalize.formatCurrency()`, which uses the default
locale.

```javascript
Globalize.locale( "en" );
Globalize.formatCurrency( 1, "USD" ); // "$9.99"
Globalize.formatCurrency( 1, "EUR" ); // "€9.99"
```

You can use the instance method `.formatCurrency()`, which uses the instance
locale.

```javascript
var de = new Globalize( "de" );

de.formatCurrency( 9.99, "USD" ); // "9,99 $"
de.formatCurrency( 9.99, "EUR" ); // "9,99 €"
```

For comparison, follow the formatting output of different symbols in different
locales.

| 3-letter currency code | en (English) | de (German) | zh (Chinese) |
| `.formatCurrency( 1, "USD" )` | `$1.00` | `1,00 $` | `US$ 1.00` |
| `.formatCurrency( 1, "EUR" )` | `€1.00` | `1,00 €` | `€ 1.00` |
| `.formatCurrency( 1, "CNY" )` | `CN¥1.00` | `1,00 CN¥` | `¥ 1.00` |
| `.formatCurrency( 1, "JPY" )` | `¥1` | `1 ¥` | `JP¥ 1` |
| `.formatCurrency( 1, "GBP" )` | `£1.00` | `1,00 £` | `£ 1.00` |
| `.formatCurrency( 1, "BRL" )` | `R$1.00` | `1,00 R$` | `R$ 1.00` |

For plural messages, use `style: "name"`.

```javascript
Globalize.formatNumber( 0, "USD", { style: "name" } );
// "0.00 US dollars"

Globalize.formatNumber( 1, "USD", { style: "name" } );
// "1.00 US dollar"
```

For comparison, follow the formatting output of different symbols in different
locales using the plural messages `Globalize( locale ).formatCurrency( 1,
currency, { style: "name" } )`.

| `USD` | `1.00 US dollar` | `1,00 US-Dollar` | `1.00美元` |
| `EUR` | `1.00 euro` | `1,00 Euro` | `1.00欧元` |
| `CNY` | `1.00 Chinese yuan` | `1,00 Chinesischer Yuan` | `1.00人民币` |
| `JPY` | `1 Japanese yen` | `1 Japanischer Yen` | `1日元` |
| `GBP` | `1.00 British pound sterling` | `1,00 Britisches Pfund Sterling` | `1.00英镑` |
| `BRL` | `1.00 Brazilian real` | `1,00 Brasilianischer Real` | `1.00巴西雷亚尔` |

For the international currency code, use `style: "code"`.

```javascript
Globalize.formatNumber( 9.99, "USD", { style: "code" } );
// "9.99 USD"
```

Globalize uses Supplemental Currency Data

Override the number of digits, grouping separators, rounding function or any
other [`.numberFormatter()` options](../number/number-formatter.md).

```javascript
Globalize.formatCurrency( 1, "USD", {
minimumFractionDigits: 0,
style: "name"
});
// "1 US dollar"

Globalize.formatCurrency( 1.491, "USD", {
round: "ceil"
});
// "$1.50"

```
10 changes: 10 additions & 0 deletions doc/error/e-missing-plural-module.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## E_MISSING_PLURAL_MODULE

Thrown when plural module is needed, but not loaded, eg. formatting currencies
using plural messages.

Error object:

| Attribute | Value |
| --- | --- |
| code | `E_MISSING_PLURAL_MODULE` |
1 change: 1 addition & 0 deletions examples/amd-bower/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ <h2>Requirements</h2>
<h2>Demo output</h2>
<p>Now: <span id="date"></span></p>
<p>A number: <span id="number"></span></p>
<p>A currency: <span id="currency"></span></p>
<p>Plurals:</p>
<ul>
<li><span id="plural-0"></li>
Expand Down
12 changes: 11 additions & 1 deletion examples/amd-bower/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,29 @@ require([
"globalize",

// CLDR content.
"json!cldr-data/main/en/currencies.json",
"json!cldr-data/main/en/ca-gregorian.json",
"json!cldr-data/main/en/numbers.json",
"json!cldr-data/supplemental/currencyData.json",
"json!cldr-data/supplemental/likelySubtags.json",
"json!cldr-data/supplemental/plurals.json",
"json!cldr-data/supplemental/timeData.json",
"json!cldr-data/supplemental/weekData.json",

// Extend Globalize with Date and Number modules.
"globalize/currency",
"globalize/date",
"globalize/number",
"globalize/plural"
], function( Globalize, enGregorian, enNumbers, likelySubtags, pluralsData, timeData, weekData ) {
], function( Globalize, enCurrencies, enGregorian, enNumbers, currencyData, likelySubtags,
pluralsData, timeData, weekData ) {

var en, pluralData;

// At this point, we have Globalize loaded. But, before we can use it, we need to feed it on the appropriate I18n content (Unicode CLDR). Read Requirements on Getting Started on the root's README.md for more information.
Globalize.load(
currencyData,
enCurrencies,
enGregorian,
enNumbers,
likelySubtags,
Expand All @@ -63,6 +70,9 @@ require([
// Use Globalize to format numbers.
document.getElementById( "number" ).innerHTML = en.formatNumber( 12345.6789 );

// Use Globalize to format currencies.
document.getElementById( "currency" ).innerHTML = en.formatCurrency( 69900, "USD" );

// Use Globalize to format a message with plural inflection.
pluralData = {
one: "{0} result",
Expand Down
5 changes: 5 additions & 0 deletions examples/node-npm/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ var Globalize = require( "globalize" );

// Before we can use Globalize, we need to feed it on the appropriate I18n content (Unicode CLDR). Read Requirements on Getting Started on the root's README.md for more information.
Globalize.load(
cldrData( "main/en/currencies" ),
cldrData( "main/en/ca-gregorian" ),
cldrData( "main/en/numbers" ),
cldrData( "supplemental/currencyData" ),
cldrData( "supplemental/likelySubtags" ),
cldrData( "supplemental/plurals" ),
cldrData( "supplemental/timeData" ),
Expand All @@ -22,6 +24,9 @@ console.log( Globalize.formatDate( new Date(), { datetime: "medium" } ) );
// Use Globalize to format numbers.
console.log( Globalize.formatNumber( 12345 ) );

// Use Globalize to format currencies.
console.log( Globalize.formatCurrency( 69900, "USD" ) );

// Use Globalize to format a message with plural inflection.
console.log( Globalize.formatPlural( 12345, {
one: "{0} result",
Expand Down
Loading

0 comments on commit a6d3246

Please sign in to comment.