dinero-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDinero.js Best Practices
Dinero.js 最佳实践
Core rules for working with Dinero.js, the JavaScript/TypeScript library for creating, calculating, and formatting money safely. Contains rules across 4 categories, prioritized by impact.
以下是使用Dinero.js的核心规则,Dinero.js是一款用于在JavaScript/TypeScript中安全创建、计算和格式化货币的库。本文包含4类按影响优先级排序的规则。
When to Apply
适用场景
Reference these guidelines when:
- Creating Dinero objects from user input, API responses, or database values
- Performing arithmetic on monetary values (adding, splitting, multiplying)
- Choosing between and
numbercalculatorsbigint - Importing from ,
dinero.js, ordinero.js/currenciesdinero.js/bigint - Working with prices, costs, taxes, or any financial calculation
在以下场景中可参考这些指南:
- 从用户输入、API响应或数据库值创建Dinero对象时
- 对货币值执行算术运算(加法、拆分、乘法)时
- 选择和
number计算器时bigint - 引入、
dinero.js或dinero.js/currencies模块时dinero.js/bigint - 处理价格、成本、税费或任何金融计算时
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Object Creation | CRITICAL | |
| 2 | Arithmetic | CRITICAL | |
| 3 | Precision | HIGH | |
| 4 | Imports | MEDIUM | |
| 优先级 | 分类 | 影响级别 | 前缀 |
|---|---|---|---|
| 1 | 对象创建 | 关键 | |
| 2 | 算术运算 | 关键 | |
| 3 | 精度处理 | 高 | |
| 4 | 模块引入 | 中 | |
Quick Reference
快速参考
1. Object Creation (CRITICAL)
1. 对象创建(关键)
- - Always pass amounts as integers in minor currency units
creation-minor-units - - Use a helper to convert float inputs to minor units
creation-from-floats - - Currencies with exponent 0 (e.g., JPY) take major units directly
creation-zero-exponent
- - 始终以货币最小单位的整数形式传入金额
creation-minor-units - - 使用辅助工具将浮点数输入转换为最小单位
creation-from-floats - - 指数为0的货币(如日元JPY)直接传入主单位
creation-zero-exponent
2. Arithmetic (CRITICAL)
2. 算术运算(关键)
- - All operations return new objects; capture the return value
arithmetic-immutability - - Use
arithmetic-allocate-not-dividefor splitting money, not manual divisionallocate - - Never multiply by a raw decimal; use scaled amounts
arithmetic-scaled-amounts - - Calculate percentages with
arithmetic-percentagesor scaledallocatemultiply
- - 所有运算都会返回新对象,请捕获返回值
arithmetic-immutability - - 使用
arithmetic-allocate-not-divide方法拆分货币,不要手动进行除法运算allocate - - 切勿直接乘以原始小数,应使用按比例缩放的数值
arithmetic-scaled-amounts - - 使用
arithmetic-percentages方法或按比例缩放的allocate计算百分比multiply
3. Precision (HIGH)
3. 精度处理(高)
- - Use
precision-bigintfor amounts exceedingdinero.js/bigintNumber.MAX_SAFE_INTEGER - - Cryptocurrencies require bigint due to high exponents
precision-crypto - - Use
precision-trim-scaleto remove trailing zeros after chained operationstrimScale
- - 当金额超过
precision-bigint时,使用Number.MAX_SAFE_INTEGERdinero.js/bigint - - 加密货币因指数较高,需使用bigint类型
precision-crypto - - 链式运算后使用
precision-trim-scale移除末尾零值trimScale
4. Imports (MEDIUM)
4. 模块引入(中)
- - Import only what you use; standalone functions enable tree-shaking
imports-tree-shaking - - Match calculator type: use
imports-bigint-currencieswithdinero.js/bigint/currenciesdinero.js/bigint
- - 仅引入所需模块;独立函数支持摇树优化(tree-shaking)
imports-tree-shaking - - 匹配计算器类型:使用
imports-bigint-currencies时搭配dinero.js/bigintdinero.js/bigint/currencies
How to Use
使用方式
Read individual rule files for detailed explanations and code examples:
rules/creation-minor-units.md
rules/arithmetic-allocate-not-divide.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
请查看单个规则文件获取详细说明和代码示例:
rules/creation-minor-units.md
rules/arithmetic-allocate-not-divide.md每个规则文件包含:
- 规则重要性的简要说明
- 错误代码示例及问题解析
- 正确代码示例及说明
- 额外背景信息和参考资料 ",