@betty-blocks/number-formatter

中文文档
2.2.0发布于 2025/02/28MITTypeScriptCommonJS零依赖近 30 天下载 2400

number formatter based on the syntax of numeral.js

原始使用文档
文档目录10 节
  1. Commands
  2. Getting started
  3. Features
  4. decimal/fractional seperator
  5. thousand seperator
  6. force sign
  7. left padding
  8. optional fractional digits
  9. functions
  10. currency

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: , . _ :

formatinputresult
0.0123.123123.1
0.000123.12313123.123
0,0123.123123,1

thousand seperator

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

formatinputresult
0,0.0123123.123123,123.1
0[]0,00123123.123123123,12
0[]0.123123.123123123
0_0:00123123.123123_123:12

force sign

formatinputresult
+0.023.123+23.1
+0.0-23.123-23.1

left padding

Adds zeroes to the left

formatinputresult
0.023.12323.1
00000.023.12300023.1
00000.23.12300023

optional fractional digits

Allow flexible numbers behind the decimal seperator.

formatinputresult
0.0[0000]23.12323.123
0.0[0000]2323.0
0.0[00]23.12323.123
0.0[00]23.1234678923.123
0.0[00]23.123.1

functions

ordinal

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

formatinputresult
0o23.12323th
0o11st

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

formatinputresult
0.0a12300001.2m
0. a12341 k
0. a-104321-104 k
0. a12341 k
0.a-104321-104k
0.000a10230000001.023b

currency

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

formatinputresult
$ 0.123.23$ 123
0. $123.23123 $
€0,00123.23€123,23
"MyCurrency" 0.00123.23MyCurrency 123.23

版本与发布记录

@betty-blocks/number-formatter 共发布 11 个版本,首次发布于 2021/04/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.2.0latest2025/02/281.7 年前
2.1.02021/05/285 年前
2.0.32021/05/125 年前
2.0.22021/04/305 年前
2.0.12021/04/295 年前
1.3.02021/04/295 年前

关于 @betty-blocks/number-formatter 的常见问题

@betty-blocks/number-formatter 是什么?

number formatter based on the syntax of numeral.js

如何安装 @betty-blocks/number-formatter?

在项目目录运行 npm install @betty-blocks/number-formatter,或使用 pnpm add / yarn add / bun add。

@betty-blocks/number-formatter 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@betty-blocks/number-formatter 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@betty-blocks/number-formatter 可以商用吗?

@betty-blocks/number-formatter 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@betty-blocks/number-formatter 的下载量有多少?

根据 npm 官方下载统计,@betty-blocks/number-formatter 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,400 次,日均约 80 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@betty-blocks/number-formatter 还在维护吗?

最新版本 2.2.0 发布于 2025/02/28(1.7 年前);至今共发布 11 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/04/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。