@audius/fixed-decimal

中文文档
0.2.1发布于 2025/12/16Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 4103

A data structure to represent fixed precision decimals

原始使用文档
文档目录5 节
  1. Description
  2. Install
  3. Usage
  4. Creating Currencies
  5. Examples

FixedDecimal

A data structure to represent fixed precision decimals.

Description

FixedDecimal is a data structure used to represent fixed precision decimals.

It's particularly useful for representing currency, especially cryptocurrency, as the underlying bigint can handle large amounts and extremely fractional amounts while keeping exact precision, and the configured decimal places count allows the class to conveniently operate on the underlying value and represent it in a user-friendly manner.

Unlike BigDecimal solutions elsewhere, FixedDecimal is not intended to be persisted and arithmetically operated on, but rather used ephemerally for normalizing and formatting. Almost all of its methods are chainable to make it convenient to initialize a FixedDecimal, operate on it, and immediately get back a string or bigint representation.

In fact, you probably won't need to initialize a FixedDecimal directly. Instead, use helper currency constructors that will ensure the input will be coerced to the proper number of decimals.

Install

npm install @audius/fixed-decimal

Usage

new FixedDecimal('12.345').toString() // '12.345'
new FixedDecimal(12.345).toString() // '12.345'
new FixedDecimal(BigInt(12345), 3).toString() // '12.345'

new FixedDecimal('12.345', 6).toString() // '12.345000'
new FixedDecimal(12.345, 6).toString() // '12.345000'
new FixedDecimal(BigInt(12345), 6).toString() // '0.012345'

new FixedDecimal(12345.6789).toLocaleString('en-US', {
  maximumFractionDigits: 2
}) // 12,345.67
new FixedDecimal(12345.6789).toLocaleString('en-US', {
  maximumFractionDigits: 2,
  roundingMode: 'halfExpand'
}) // 12,345.68

Creating Currencies

If, for example, you want to use the 18 decimal ETH in your code, you could create a helper like:

const ETH = (value) => new FixedDecimal(value, 18)
// Calling .value will get the wei amount
console.log(ETH('1.42')).value // 1420000000000000000n

// Can also go from wei to UI string
console.log(
  ETH(12345678901234567890n).toLocaleString('en-US', {
    maximumFractionDigits: 2
  })
) // 12.34

More examples and documentation can be found in the JSDoc and in the tests.

Examples

See the ./examples folder for examples on usage of this package.

Run with

npx tsx examples/audio.ts

版本与发布记录

@audius/fixed-decimal 共发布 8 个版本,首次发布于 2023/12/23;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.2.1latest2025/12/1610 个月前
0.2.02025/08/061.3 年前
0.1.12024/10/042 年前
0.1.02024/09/182 年前
0.0.252024/06/012 年前
0.0.242024/04/122 年前

关于 @audius/fixed-decimal 的常见问题

@audius/fixed-decimal 是什么?

A data structure to represent fixed precision decimals

如何安装 @audius/fixed-decimal?

在项目目录运行 npm install @audius/fixed-decimal,或使用 pnpm add / yarn add / bun add。

@audius/fixed-decimal 支持 TypeScript 吗?

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

@audius/fixed-decimal 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@audius/fixed-decimal 可以商用吗?

@audius/fixed-decimal 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@audius/fixed-decimal 的下载量有多少?

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

@audius/fixed-decimal 还在维护吗?

最新版本 0.2.1 发布于 2025/12/16(10 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 1 个;首次发布于 2023/12/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。