diff --git a/README.md b/README.md index 85197dc..b352430 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,13 @@ currency.toString() // €22.32 ``` +`options`: accepts options object that will be forwarded to `Intl.NumberFormat` + +```javascript +new LocalCurrency.Currency({ amount: 20 }).toString({ maximumFractionDigits: 0 }) +// $20 +``` + ### fromUSD Converts USD into local currency: diff --git a/dist/esm/index.js b/dist/esm/index.js index d8fc929..8b70e8f 100644 --- a/dist/esm/index.js +++ b/dist/esm/index.js @@ -460,8 +460,8 @@ class Currency { return Intl.DateTimeFormat().resolvedOptions().timeZone } - toString() { - return new Intl.NumberFormat(navigator.language, { + toString(options = {}) { + return new Intl.NumberFormat(navigator.language, {...options, style: 'currency', currency: this.code, }).format(this.amount) diff --git a/dist/umd/index.js b/dist/umd/index.js index 47c1614..7c538d9 100644 --- a/dist/umd/index.js +++ b/dist/umd/index.js @@ -466,8 +466,8 @@ return Intl.DateTimeFormat().resolvedOptions().timeZone } - toString() { - return new Intl.NumberFormat(navigator.language, { + toString(options = {}) { + return new Intl.NumberFormat(navigator.language, {...options, style: 'currency', currency: this.code, }).format(this.amount) diff --git a/package.json b/package.json index 38a9733..bc9ae03 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@depay/local-currency", "moduleName": "LocalCurrency", - "version": "3.3.0", + "version": "3.4.0", "description": "JavaScript library that detects user's local currency and provides functionalities to convert between multiple currencies.", "main": "dist/umd/index.js", "module": "dist/esm/index.js", diff --git a/src/Currency.js b/src/Currency.js index 06816cd..fcecd60 100644 --- a/src/Currency.js +++ b/src/Currency.js @@ -35,8 +35,8 @@ class Currency { return Intl.DateTimeFormat().resolvedOptions().timeZone } - toString() { - return new Intl.NumberFormat(navigator.language, { + toString(options = {}) { + return new Intl.NumberFormat(navigator.language, {...options, style: 'currency', currency: this.code, }).format(this.amount)