Skip to main content

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

NameTypeDefaultDescription
localestringENFormat thousand and decimal separator.
currencystringUSDFormat symbol by currency:
usd ($), vnd (đ)
ratio1 , 100, 1000 , 100001
currencyDisplaynone , symbolnoneShow symbol by currency. EX: 1,000$
symbolPositionbefore , afterafterShow symbol after or before value.
Ex: before -> $1,000
symbolPrefixnone ,roundBracketsnoneIf use roundBrackets to 1,000($)
notationstandard ,compactstandardShorthand number.
Ex: 123456 -> 123K
maximumFractionDigitsnumber0Maximum Fraction digits.
Ex: maximumFractionDigits: 2 => 123456 -> 123.45K
decimalSeparatorstring.Determine separator for decimal
thousandSeparatorstring,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