npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

box-amount

v1.0.4

Published

[🎁 JavaScript 工具盒] 金额处理

Readme

box-amount

[🎁 JavaScript 工具盒] 金额处理

  • yuanToFen 元转分
  • fenToYuan 分转元
  • percentageOf 百分比计算
  • percent 添加百分号
  • currency 格式化为货币
  • validate 验证金额
  • ok 断言验证通过
  • plus 安全加法计算
  • minus 安全减法计算
  • times 安全乘法计算
  • div 安全除法计算
  • pipe 自定义管道处理

安装

npm i box-amount

使用

import Amount from 'box-amount'

// 组合操作
const result1 = new Amount(10.5)
  .yuanToFen() // 1050
  .plus(100) // 1150
  .fenToYuan(2) // 11.5
  .currency() // ¥11.50
  .result()
console.log('result1', result1) // ¥11.50

// 将金额格式化为货币
const result2 = new Amount(1000055)
  .fenToYuan() // 10000.55
  .currency({
    prefix: '¥',
    showPrefix: true,
    suffix: '元',
    showSuffix: true,
    decimal: 2
  }) // ¥10,000.55元
  .result()
console.log('result2', result2) // ¥10,000.55元

// 百分比计算
const result3 = new Amount(10)
  .percentageOf(50) // 20
  .percent() // 20%
  .result()
console.log('result3', result3) // 20%

// 验证金额
// const result4 = new Amount(-10.111)
//   .validate({ min: 10, max: 20, decimal: 2, allowNegative: false })
//   .ok() // Uncaught Error: 验证失败:金额不能为负数, 金额不能小于10, 金额最多只能有2位小数

const result4 = new Amount(11)
  .validate({ min: 10, max: 20, decimal: 2, allowNegative: false })
  .ok() // 验证通过
  .plus(1) // 12
  .result()
console.log('result4', result4) // 12

// 小数安全计算
const result5 = new Amount(0.1)
  .plus(0.2) // 0.3
  .times(10) // 3
  .minus(1) // 2
  .div(0.2) // 10
  .percent() // 10%
  .result()
console.log('result5', result5) // 10%

// 自定义管道处理
const result6 = new Amount(1)
  .pipe(val => val + 1) // 2
  .plus(1) // 3
  .pipe(val => `${val}积分`) // 3积分
  .result()
console.log('result6', result6) // 3积分

// 重写 valueOf toString 可以直接获取结果
const result8 = +new Amount(10).plus(1)
console.log('result8', result8) // 11
const result9 = `${new Amount(10).percent()}`
console.log('result9', result9) // 10

// 查看操作日志
const result7 = new Amount(0.1)
  .plus(0.2)
  .times(10)
  .percent()
  .log() // 传 true 可在控制台打印日志
console.log('result7', result7)
// [
//   {
//     "operation": "plus",
//     "input": 0.1,
//     "output": 0.3,
//     "params": {
//       "value": 0.2
//     },
//     "timestamp": "YYYY-MM-DD HH:mm:ss"
//   },
//   {
//     "operation": "times",
//     "input": 0.3,
//     "output": 3,
//     "params": {
//       "value": 10
//     },
//     "timestamp": "YYYY-MM-DD HH:mm:ss"
//   },
//   {
//     "operation": "percent",
//     "input": 3,
//     "output": "3%",
//     "params": {},
//     "timestamp": "YYYY-MM-DD HH:mm:ss"
//   }
// ]

将金额格式化为货币等形式后,勿再进行计算等操作