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 all commits
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
33 changes: 33 additions & 0 deletions packages/localize-monetary-amount/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# localize-monetary-amount

Locale- (language and geo) and currency-aware formatting of exact monetary amounts. Designed for displaying product prices in catalogs, carts, checkout, and receipts.

What this package does:
* Number and currency localization (separator symbols, grouping conventions)
* Use fixed point arithmetic internally -- no floats or rounding.

What this package doesn't do:
* Arithmetic on monetary amounts; formatting only
* Conversion between currencies
* Localization of digit symbols -- Hindu-Arabic digits only. (Lifting this restriction is in scope, but currently not implemented)
* Take options. Locale preferences are hardcoded, please submit an issue if a locale looks off.
* Use any external dependencies

## Example usage

To format a monetary amount you'll need the following:
* 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`. This is typically the format browsers use for the user's locale.
* 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';

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

// Use 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)' ] };
38 changes: 38 additions & 0 deletions packages/localize-monetary-amount/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"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",
"types": "types/index.d.ts",
"sideEffects": false,
"scripts": {
"clean": "npx rimraf dist types",
"prepublish": "npm run clean",
"prepare": "tsc --project ./tsconfig.json && tsc --project ./tsconfig-cjs.json"
},
"files": [
"dist",
"src",
"types"
],
"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