MyConverter
We have provide a utils to convert anything as string, number, date... Gitlab
Installation
- Install via pnpm
pnpm add @marketplace/myconverter
- Install via yarn
yarn add @marketplace/myconverter
- Install via npm
npm install @marketplace/myconverter
Convert Number
We have a namespace with name: NumberConverter
Usage
Method: setting()
Allow setting all anything as global.
setting(op: Partial<NumberConverterOption>)
More: NumberConverterOption Type
Example:
import { NumberConverter } from "@marketplace/myconverter";
NumberConverter.setting({ currency: "usd", locale: "en" });
Method: format()
Format number to string
format(num?: number | bigint, option?: Partial<NumberConverterOption>): string;
Example:
import { NumberConverter } from "@marketplace/myconverter";
NumberConverter.format(123456); // Result: 123,456
Custom options
import { NumberConverter } from "@marketplace/myconverter";
NumberConverter.format(123456, {
currencyDisplay: "symbol",
symbolPosition: "before",
currency: "vnd",
locale: "vi",
}); // Result: ₫123.456
// Use compact
NumberConverter.format(123456, {
currencyDisplay: "symbol",
symbolPosition: "before",
currency: "vnd",
notation: "compact",
locale: "vi",
}); // Result: ₫123K
More: NumberConverterOption Type
Types
NumberConverterOption
| Name | Type | Default | Description |
|---|---|---|---|
| locale | string | EN | Format thousand and decimal separator. |
| currency | string | USD | Format symbol by currency: usd ($), vnd (đ) |
| ratio | 1 , 100, 1000 , 10000 | 1 | |
| currencyDisplay | none , symbol | none | Show symbol by currency. EX: 1,000$ |
| symbolPosition | before , after | after | Show symbol after or before value. Ex: before -> $1,000 |
| symbolPrefix | none ,roundBrackets | none | If use roundBrackets to 1,000($) |
| notation | standard ,compact | standard | Shorthand number. Ex: 123456 -> 123K |
| maximumFractionDigits | number | 0 | Maximum Fraction digits. Ex: maximumFractionDigits: 2 => 123456 -> 123.45K |
| decimalSeparator | string | . | Determine separator for decimal |
| thousandSeparator | string | , | Determine separator for thousand |
CurrencyHelper
Instead of using the NumberConverter directedly, we can use CurrencyHelper and CurrencyPattern with pre-define configs
Method: setting()
CurrencyHelper.setting({
currency: "usd",
ratio: 1000,
});
Method: doFormat()
doFormat(num: number, pattern: CurrencyPattern, customFormat?: Partial<NumberConverterOption>): string;
We can also put some custom format properties if needed
CurrencyHelper.doFormat(999999999, CurrencyPattern.Wallet);
References
- List of Circulating Currencies: https://github.com/ourworldincode/currency