Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

localize-currency-amount: Utility for locale-aware currency formatting #36758

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/localize-monetary-amount/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# localize-monetary-amount

Locale- (language and geo) and currency-aware formatting of exact monetary amounts.

What this package does:
* Number localization (separator symbols, grouping conventions)
* Integer arithmetic with all digits of precision. No floats.
* Designed for displaying product prices in catalogs, carts, checkout, and receipts.
* No dependencies

What this package doesn't do:
* Arithmetic on monetary amounts; formatting only
* Conversion between currencies
* Localize digit symbols (Hindu-Arabic digits only)
* Take options. Locale preferences are hardcoded. Please submit an issue if a locale looks off.

## Example usage

You'll need:
* An ISO 631-1 language code with an optional ISO 3166-1 alpha-2 region code, separated by a hyphen. Examples: `en`, `en-gb`, `fr-be`.
* An ISO 4217 currency code. Examples: 'USD', 'JPY', 'BRL'.
* An integer number of _minor units_. This is the minimal unit of your currency; e.g. cents for USD, yen for JPY. May be positive or negative.

If the fractional part is zero it is omitted unless the locale prefers otherwise.

```js
import localizeMonetaryAmount from 'localize-monetary-amount';

// Strips zero minor units:
localizeMonetaryAmount( 'en-us', 'USD', 500 ); // '$5'

// Uses non-breaking spaces:
localizeMonetaryAmount( 'fr-ca', 'CAD', 500000 ); // '$5\u00A0000\u00A0CAD'
```
1 change: 1 addition & 0 deletions packages/localize-monetary-amount/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { rootDir: __dirname, testMatch: [ '**/test/**/*.[jt]s?(x)' ] };
35 changes: 35 additions & 0 deletions packages/localize-monetary-amount/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@automattic/localize-monetary-amount",
"version": "1.0.0",
"description": "Batteries included localized currency formatting",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"sideEffects": false,
"scripts": {
"clean": "npx rimraf dist",
"prepublish": "npm run clean"
},
nbloomf marked this conversation as resolved.
Show resolved Hide resolved
"files": [
"dist",
"src"
nbloomf marked this conversation as resolved.
Show resolved Hide resolved
],
"keywords": [
"localization",
"currency",
"automattic"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Automattic/wp-calypso.git",
"directory": "packages/localize-monetary-amount"
},
"author": "@automattic",
"license": "GPL-2.0-or-later",
"bugs": {
"url": "https://github.com/Automattic/wp-calypso/issues"
},
"homepage": "https://github.com/Automattic/wp-calypso/tree/master/packages/localized-monetary-amount#readme"
}
Loading