fmt-money
v1.0.7
Published
A utility for formatting money values with commas and decimals.
Readme
fmt-money;
一个简单的工具,用于将数字格式化为货币值,添加千分位分隔符,并支持任意位小数。
A simple tool for formatting numbers as currency values, adding thousand separators, and supporting any number of decimal places.
安装
通过 npm 安装:
Install via npm:
npm install fmt-money;
API
formatMoney(amount, decimalCount = 2) 通过添加千分位分隔符和四舍五入到指定的小数位数,格式化给定的金额。
Formats a given amount by adding thousand separators and rounding to the specified decimal places.
参数 amount (number): 要格式化的金额。必须是有效的数字。 decimalCount (number | string, 可选): 要显示的小数位数。默认为 2。可以是数字或数字字符串。
返回值 格式化后的货币值字符串表示。例如,1234.56 变为 "1,234.56"。 A formatted string representation of the monetary value. For example, 1234.56 becomes "1,234.56".
错误处理 如果 amount 不是有效的数字,函数将记录错误:Invalid input: amount must be a number. 如果 decimalCount 不是有效的非负数字或数字字符串,函数将记录错误:Invalid input: decimalCount must be a number or a numeric string.
If amount is not a valid number, the function will log an error: Invalid input: amount must be a number. If decimalCount is not a valid non-negative number or a numeric string, the function will log an error: Invalid input: decimalCount must be a number or a numeric string.
示例
import formatMoney from 'fmt-money';
formatMoney(1234567.891); // "1,234,567.89" formatMoney(1234567.891, 1); // "1,234,567.9" formatMoney(1234567, 0); // "1,234,567"
