2.0.5 • Published 29 days ago

@dreamworld/dw-currency v2.0.5

Weekly downloads
17
License
ISC
Repository
github
Last release
29 days ago

Dw currency

dw-currency-format

Install

yarn add @dreamworld/dw-currency

or

npm install @dreamworld/dw-currency

Usage

import '@dreamworld/dw-currency-format';
import { DwCurrency } from '@dreamworld/dw-currency';
<dw-currency-format value=${1500} currency=${USD}></dw-currency-format>

DwCurrency Methods

MethodArgumentsReturns
formatvalue (Number) or Object (e.g. {value: Number, currency: String, decimalPoints: Number, noNegative: Boolean, noExtraDecimalZero: Boolean})(String): Returns amount in string
formatWithSymbolvalue (Number) or Object (e.g. {value: Number, currency: String, position: String})(String): Returns amount in string with currency symbol

Usage

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 €

Properties

PropertyTypeDescription
valueNumberCurrency Value which is to be formatted.
currencyStringIso code of currency
symbolFormatString (enum)Position of the symbol. Possible values: none, prefix and postfix. Default: prefix.
decimalPointsNumberNumber 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.
noNegativeBooleanWhen true doesn't shown negative sign
noExtraDecimalZeroBooleanWhen true doesn't shown extra decimal zero

Global Configuration

With the library we have shipped few currencies configuration, see currency-config.js. That should be sufficient for most of the use-case.

Change global configuration

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
    }
});

Currency Config

Configuration nameTypeDescription
symbolStringCurrency Symbol
thousandSeparatorStringCharacter to be used as thousand separator. Default value: , (comma)
decimalSeparatorStringCharacter to be used as decimal separator. Default value: . (dot)
thousandSpacingString (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.
symbolStyleObjectCSS Styles to be applied to symbol. e.g {color: 'green'}. It turns symbol into green color. This is mostly used to set custom/required font for the symbol.
decimalPointsNumberPrecision. Number of digits after decimal points. Default value: 2.
valueDividerNumber1Actual value of the currency = value/valueDivider. Default value: 1. E.g. In Hisab currency value is non-floating point (integer, actually long) number. It's possible becuase, currency value is considered in Paisa/Cent. So, It's actual value is found by dividing it with 100. In such case you need to set valueDivider to 100.

Change Defaults

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
});

setDefaultCurrency

DwCurrency.setDefaultsCurrrency("INR");