source: src/utils/format-number.ts

main
Last change on this file was 5d6f37a, checked in by Naum Shapkarovski <naumshapkarovski@…>, 7 weeks ago

add customer

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import numeral from 'numeral';
2
3// ----------------------------------------------------------------------
4
5type InputValue = string | number | null;
6
7export function fNumber(number: InputValue) {
8 return numeral(number).format();
9}
10
11export function fCurrency(number: InputValue, currency: 'EUR' | 'USD' = 'EUR'): string {
12 const parsedNumber = parseFloat(String(number));
13
14 const locale = currency === 'USD' ? 'en-US' : 'de-DE';
15 const formatter = new Intl.NumberFormat(locale, {
16 style: 'currency',
17 currency,
18 });
19
20 return formatter.format(parsedNumber);
21}
22
23export function fPercent(number: InputValue) {
24 const format = number ? numeral(Number(number) / 100).format('0.0%') : '';
25
26 return result(format, '.0');
27}
28
29export function fShortenNumber(number: InputValue) {
30 const format = number ? numeral(number).format('0.00a') : '';
31
32 return result(format, '.00');
33}
34
35export function fData(number: InputValue) {
36 const format = number ? numeral(number).format('0.0 b') : '';
37
38 return result(format, '.0');
39}
40
41function result(format: string, key = '.00') {
42 const isInteger = format.includes(key);
43
44 return isInteger ? format.replace(key, '') : format;
45}
Note: See TracBrowser for help on using the repository browser.