@betty-blocks/number-formatter
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

Number Formatter

number formatter based on the syntax of numeral.js

The library works like:

  • parse user format
  • create config from parsed format
  • format using the generated config

The parser is generated from a pegjs grammar.

Commands

After changing the grammar you need to regenerate the parser the grammar.pegjs:

yarn generate

Run tests:

yarn test

Run formatter (prettier):

yarn fmt

Getting started

import { createConfig, format } from "@betty-blocks/number-formatter";

const myFormat = "0_0,00";

// create a config from your format
const { config } = createConfig(myFormat);

const myNumber = 123123.123;

// prints 123_123,12
console.log(format(myNumber, config));

other example:

import { createConfig, format } from "@betty-blocks/number-formatter";

const formatNumber = (number: number, formatConfig: string): string => {
  let { config, error } = createConfig(formatConfig);
  if (error) {
    return error;
  }
  return format(number, config);
};
import { createConfig, format } from "@betty-blocks/number-formatter";
import { Config } from "@betty-blocks/number-formatter/src/types";

const DEFAULT_FORMAT = "0.00";

const formatNumbers = (numbers: number[], formatConfig: string): string[] => {
  let {config, error} = createConfig(formatConfig);
  if (error) {
    {config} = createConfig(DEFAULT_FORMAT)
  }

  return numbers.map((number) => format(number, config));
};

Features

decimal/fractional seperator

Will use the first symbol that is encountered current allowed symbols are one of: , . _ :

format input result
0.0 123.123 123.1
0.000 123.12313 123.123
0,0 123.123 123,1

thousand seperator

Will use the first occurrence of a symbol between [] or if two decimal seperators are encountered.

format input result
0,0.0 123123.123 123,123.1
0[]0,00 123123.123 123123,12
0[]0. 123123.123 123123
0_0:00 123123.123 123_123:12

force sign

format input result
+0.0 23.123 +23.1
+0.0 -23.123 -23.1

left padding

Adds zeroes to the left

format input result
0.0 23.123 23.1
00000.0 23.123 00023.1
00000. 23.123 00023

optional fractional digits

Allow flexible numbers behind the decimal seperator.

format input result
0.0[0000] 23.123 23.123
0.0[0000] 23 23.0
0.0[00] 23.123 23.123
0.0[00] 23.12346789 23.123
0.0[00] 23.1 23.1

functions

ordinal

Or postition, rank ect. Postfixes 1, 2 and 3 with st, nd and rd other numbers get the th postfix

format input result
0o 23.123 23th
0o 1 1st

average

if a number is less than 1000 get no postfix if a number is between 1000 and 999999 gets a k postfix if a number is greater than 1000000 gets a m postfix

format input result
0.0a 1230000 1.2m
0. a 1234 1 k
0. a -104321 -104 k
0. a 1234 1 k
0.a -104321 -104k

currency

Any text that is encountered will be used as a currency symbol

format input result
$ 0. 123.23 $ 123
0. $ 123.23 123 $
€0,00 123.23 €123,23
"MyCurrency" 0.00 123.23 MyCurrency 123.23

Readme

Keywords

none

Package Sidebar

Install

npm i @betty-blocks/number-formatter

Weekly Downloads

113

Version

2.1.0

License

MIT

Unpacked Size

115 kB

Total Files

66

Last publish

Collaborators

  • maxpijnacker
  • invoicebetty
  • nickruigrok-betty
  • dylan.carver
  • bettyblocks
  • ingmar.betty
  • jorispannekeet
  • jordyqbetty
  • jeroenreijs
  • michelryan
  • davidbehal
  • peter1984
  • emielwit
  • thomas.timmer
  • pme-legend
  • stefanvd
  • thomas.vanophem
  • menouern