# @half0wl/money 中文文档

规范地址：https://npmcn.com/package/%40half0wl/money/

版本：1\.0\.2
npm 元数据更新时间：2022-12-12T17:11:12.041Z
中文译文更新时间：2026-11-17T06:40:14.748Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40half0wl/money
原文对照：https://npmcn.com/package/%40half0wl/money/?lang=original
项目主页：https://github.com/half0wl/ts-money#readme
源码仓库：https://github.com/half0wl/ts-money

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：60.1 kB，21 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2022-12-03T23:11:53.468Z
- 最新版本发布：2022-12-12T17:11:11.920Z
- 近 30 天下载量：6298（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：3（近 12 个月 0 个）

## 中文简介

一个TypeScript/JavaScript库,使货币价值的工作更加容易和安全.

## 安装

```sh
npm install @half0wl/money
```

也可以使用：`pnpm add @half0wl/money`、`yarn add @half0wl/money`、`bun add @half0wl/money`

## README

# 钱 特斯

![CI](https://github.com/half0wl/money.ts/actions/workflows/ci.yml/badge.svg) [![NPM](https://img.shields.io/npm/v/@half0wl/money)](https://www.npmjs.com/package/@half0wl/money) [![Documentation](https://img.shields.io/badge/Documentation-%F0%9F%93%9A-orange)](https://half0wl.github.io/money.ts/index.html)

这个库旨在让TypeScript/JavaScript中的货币值工作更加安全,更容易.

货币价值总是有货币. 它们按货币区分(1个是) 不等于1欧元,每种货币都有不同的属性,影响 如何显示,四舍五入,分析。 得到所有东西可能很困难 如果你是处理钱, 得到它可能很贵 有什么不对劲!

见: [http://martinfowler.com/eaaCatalog/money.html (中文(简体) )](http://martinfowler.com/eaaCatalog/money.html)

## 安装

```sh
# Using npm:
$ npm i @half0wl/money

# Using yarn:
$ yarn add @half0wl/money
```

### 导入

```typescript
// TypeScript
import { Money } from "@half0wl/money"

// JavaScript/CommonJS
'use strict';
const Money = require("@half0wl/money").Money
```

## 特征

-   算术(加,减等)和比较(等分,大/小等)
-   不可磨灭. 每个操作返回一个新的 `Money` 对象,属性不可修改
-   地方意识字符串格式化
-   轻量级; 无外部依赖性
-   JavaScript 已完全支持. 启用 [严格模式](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) (单位:千美元)`'use strict';`)是高度推荐的,但是没有它,这将会是好的
-   [完整文档](https://half0wl.github.io/money.ts/index.html)

## 概念

金钱对象有一个 `currency` 财务报告和审定财务报表 `amount`,其中:

-   货币是一种 [ISO-4217 标准](https://www.xe.com/iso4217.php) α代码,例如:. `USD`, (中文(简体) ), `JPY`, (中文(简体) ), `EUR`, (中文(简体) ), `SGD`
-   数额以货币最低单位表示,例如5.00美元=500美分 `{ currency: USD, amount: 500 }`

## 使用量

完整的 API 文档 `Money` 类可用 [这里](https://half0wl.github.io/money.ts/classes/Money.html#constructor)。 。 。 .

```typescript
import { Money } from "@half0wl/money"

/**
 * Basic operations
 */
const listing = new Money('USD', 19999)                // Default constructor from int
listing.getCurrency()                                  // => USD
listing.getAmount()                                    // => 19999
listing.toLocaleString()                               // => '$199.99'

const shipping = Money.fromFloat('USD', 15.00)         // From float
shipping.getAmount()                                   // => 1500
shipping.toLocaleString()                              // => '$15.00'

const xmasDiscount = Money.fromString('USD', '2000')   // From int-string
xmasDiscount.getAmount()                               // => 2000
xmasDiscount.toLocaleString()                          // => '$20.00'

const promoDiscount = Money.fromString('USD', '10.00') // From float-string
promoDiscount.getAmount()                              // => 1000
promoDiscount.toLocaleString()                         // => '$10.00'

const subtotal = listing
  .add(shipping)
  .subtract(xmasDiscount, promoDiscount)
subtotal.getCurrency()                                 // => 'USD'
subtotal.getAmount()                                   // => 18499
subtotal.toLocaleString()                              // => '$184.99'

/**
 * Other arithmetic operations
 */
subtotal.multiply(2.4).toLocaleString()                // => '$443.98'
subtotal.divide(8).toLocaleString()                    // => $23.12

/**
 * Comparisons
 */
promoDiscount.equals(promoDiscount)                    // => false
promoDiscount.greaterThan(promoDiscount)               // => true
shipping.greaterThanOrEqual(listing)                   // => false
shipping.lessThan(listing)                             // => true

/**
 * Formatting
 */
const tenDollars = new Money('USD', 1000)
// Using default locale (`en-US`):
tenDollars.toLocaleString()                            // => $10.00
// Using different locale:
tenDollars.toLocaleString('fr-CA')                     // => 10,00 $ US
// JSON
tenDollars.toJSON()                                    // => {
                                                       //      currency: 'USD',
                                                       //      amount: 1000
                                                       //    }

/**
 * Immutability
 */
// All operations return a new `Money` object:
const sixEur = new Money('EUR', 600)
const oneEur = new Money('EUR', 100)
sixEur.add(oneEur)       // This returns a new Money object!
sixEur.toLocaleString()  // => '€6.00'
                         // The amount value is *not* updated in-place.

// To get the result of `sixEur.add(oneEur)`:
const result = sixEur.add(oneEur)
result.toLocaleString()  // => '€7.00'

// Properties are *always* read-only:
// TypeScript
const twoEur = new Money('EUR', 200)
twoEur['amount'] = 0 // => error TS2540: Cannot assign to 'amount'
                     //    because it is a read-only property.

// JavaScript (in strict mode)
'use strict';
const threeEur = new Money('EUR', 300)
threeEur['amount'] = 0 // => TypeError: Cannot assign to read only
                       //    property 'amount' of object '#<Money>'
```

## 发展

复制并安装依赖性 :

```sh
$ git clone https://github.com/half0wl/money.ts.git
$ yarn install
```

试验位于 [`/tests`](https://npmcn.com/tests)运行它们:

```sh
$ yarn test
```

代码文档使用 [类型文件](https://typedoc.org/)。 。 。 . 要生成文件 :

```sh
$ yarn gen-docs
```

## 鸣谢

-   [货币/货币](https://github.com/moneyphp/money) 用于启发
-   [达达胡布](https://datahub.io/core/currency-codes) 货币数据

## 许可证

[麻省理工学院](https://npmcn.com/package/%40half0wl/money/LICENSE)

## 常见问题

### @half0wl/money 是什么？

一个TypeScript/JavaScript库,使货币价值的工作更加容易和安全\.

### 如何安装 @half0wl/money？

在项目目录运行 npm install @half0wl/money，或使用 pnpm add / yarn add / bun add。

### @half0wl/money 支持 TypeScript 吗？

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

### @half0wl/money 支持 ESM 还是 CommonJS？

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

### @half0wl/money 可以商用吗？

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

### @half0wl/money 的下载量有多少？

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

### @half0wl/money 还在维护吗？

最新版本 1\.0\.2 发布于 2022/12/13（3 年前）；至今共发布 3 个版本，其中过去 12 个月发布了 0 个；首次发布于 2022/12/04。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

