yarn add @dreamworld/dw-currency
or
npm install @dreamworld/dw-currency
import '@dreamworld/dw-currency-format';
import { DwCurrency } from '@dreamworld/dw-currency';
<dw-currency-format value=${1500} currency=${USD}></dw-currency-format>
Method | Arguments | Returns |
---|---|---|
format | value (Number) or Object (e.g. {value: Number, currency: String, decimalPoints: Number, noNegative: Boolean, noExtraDecimalZero: Boolean}) | (String): Returns amount in string |
formatWithSymbol | value (Number) or Object (e.g. {value: Number, currency: String, position: String}) | (String): Returns amount in string with currency symbol |
DwCurrency.format(150023)
//return 1500.23
DwCurrency.formatWithSymbol(150023)
//return ₹ 1500.23
DwCurrency.formatWithSymbol({value: 150023, symbol: "EUR"})
//return € 1500.23
DwCurrency.formatWithSymbol({value: 150023, symbol: "EUR", position: "postfix"})
//return 1500.23 €
Property | Type | Description |
---|---|---|
value | Number | Currency Value which is to be formatted. |
currency | String | Iso code of currency |
symbolFormat | String (enum) | Position of the symbol. Possible values: none , prefix and postfix . Default: prefix . |
decimalPoints | Number | Number of the decimal points to be shown. Default value is used from the global configuration. But, sometimes, we may need to override it at the time of usage. e.g. Set it to 0 to show no decimal at all. |
noNegative | Boolean | When true doesn't shown negative sign |
noExtraDecimalZero | Boolean | When true doesn't shown extra decimal zero |
With the library we have shipped few currencies configuration, see currency-config.js
. That should be sufficient for most of the use-case.
Sample configuration for a (single) currency is as follows:
{
symbol: "₹",
thousandSeparator: ',',
decimalSeparator: '.',
thousandSpacing: '2s',
decimalPoints: 2,
valueDivider: 1
}
We can change a configuration for any existing currency, or add a new currency to the config as follows.
import { DwCurrency } '@dreamworld/dw-currency';
DwCurrency.setConfig({
USD: {
symbol: '$',
thousandSpacing: '3'
},
MYCURRENCY: {
symbol: "MC",
valueDivider: 100
}
});
Configuration name | Type | Description |
---|---|---|
symbol | String | Currency Symbol |
thousandSeparator | String | Character to be used as thousand separator. Default value: , (comma) |
decimalSeparator | String | Character to be used as decimal separator. Default value: . (dot) |
thousandSpacing | String (enum) | How many numbers are grouped for thousand separator. Possible values are 2 , 2s , 3 , 4 . Default value: 3 . Whne 2 will format like 1,23,45,67,89. When 3 will format like 1,234,567,981. When 2s will format like 1,23,45,67,981. When 4 will format like 1,2345,6789. |
symbolStyle | Object | |
decimalPoints | Number | Precision. Number of digits after decimal points. Default value: 2 . |
valueDivider | Number | 1 |
Default config for all the currencies are as follows. It's defined in currency-config.js
.
{
thousandSeparator: ',',
decimalSeparator: '.',
thousandSpacing: '3',
decimalPoints: 2,
valueDivider: 1
}
And this defaults can be overriden for your application (at global level), using following:
import { DwCurrency } '@dreamworld/dw-currency';
DwCurrency.setDefaults({
thousandSeparator: ',',
decimalSeparator: '.',
thousandSpacing: '3',
decimalPoints: 2,
valueDivider: 100
});
DwCurrency.setDefaultsCurrrency("INR");