@tarsiidae/fn
v1.0.5
Published
A TypeScript Function Library
Downloads
144
Maintainers
Readme
@tarsiidae/fn
业务中常用的函数库
安装
yarn add @tarsiidae/fn
npm install @tarsiidae/fn
pnpm add @tarsiidae/fn使用
formatThousand - 千分位格式化
将数字格式化为带千分位分隔符的字符串,支持截断小数位。
import { formatThousand } from '@tarsiidae/fn';
// 基本使用
console.log(formatThousand(1234567.89)); // "1,234,567.89"
console.log(formatThousand(1000.123)); // "1,000.12"
console.log(formatThousand('1234567.89')); // "1,234,567.89"
// 自定义小数位数
console.log(formatThousand(1234567.89, { decimal: 3 })); // "1,234,567.890"
console.log(formatThousand(1234567.89, { decimal: 1 })); // "1,234,567.8"
console.log(formatThousand(1234567.89, { decimal: 0 })); // "1,234,567"
// 不显示千分位分隔符
console.log(formatThousand(1234567.89, { showSeparator: false })); // "1234567.89"
console.log(formatThousand(1000.123, { showSeparator: false })); // "1000.12"
// 组合使用
console.log(formatThousand(1234567.89, {
decimal: 3,
showSeparator: false
})); // "1234567.890"参数说明
formatThousand(value, options?)
- value
number | string | undefined- 要格式化的数值 - options
object(可选) - 配置选项- decimal
number(可选) - 保留的小数位数,默认为 2 - showSeparator
boolean(可选) - 是否显示千分位分隔符,默认为 true
- decimal
特性
- ✅ 截断小数 - 使用截断而非四舍五入,避免精度问题
- ✅ 千分位分隔符 - 自动添加千分位分隔符,提高可读性
- ✅ 灵活配置 - 支持自定义小数位数和分隔符显示
- ✅ 类型安全 - 完整的 TypeScript 类型支持
- ✅ 字符串支持 - 支持数字字符串输入
注意事项
- 小数位采用截断方式,例如
1000.125保留两位小数结果为1000.12 - 当
decimal为 0 时,会截断所有小数部分 - 支持
undefined和无效字符串输入,会返回0.00
许可证
MIT
