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

@wzo/calc

v0.0.2

Published

Precision math + number formatting — zero runtime dependencies, BigInt internally

Readme

JavaScript 原生浮点会算错(0.1 + 0.2 === 0.30000000000000004)。@wzo/calc 用 BigInt 做严格的十进制有理运算,并提供类型友好的数字格式化。

✨ 特性

  • 🎯 精度算术 —— 内部以 digits × 10^(-exp) 表示,所有有理运算严格正确
  • 🧮 表达式求值 —— calc('1 + 2 * 3'),纯算术 + 数学函数(abs/min/max/sqrt/pow/mod/clamp…),变量用模板插值
  • 🪢 计算 / 展示分离 —— calc 出错抛异常(计算用);fmt 是 calc 的展示版,支持运算且出错走 _error 兜底(模板渲染用)
  • 🎨 类型友好的格式化 —— 千分位、百分比、压缩、分数、科学记数、6 种舍入,全用 IFormat 对象配置(IDE 补全、写错即报错)
  • 🔗 链式 / 独立函数 / 聚合 等多种 API 风格
  • 📦 0 运行时依赖,浏览器 / Node 都能跑,完整 TypeScript 类型

📦 安装

pnpm add @wzo/calc

🚀 快速上手

import { calc, calcSum, chainAdd, fmt } from '@wzo/calc'

// 精度算术
calc('0.1 + 0.2') // "0.3"

// 模板插值代替变量 + 格式化(IFormat 对象)
calc('9.9 * 3', { _fmt: { decimals: 2 } }) // "29.70"

// 直接格式化
fmt(1234567, { decimals: 2, thousands: true }) // "1,234,567.00"
fmt(1234567, { compact: 'zh' }) // "123.4567万"

// 链式
chainAdd(10).sub(3).mul(2)() // "14"

// 聚合
calcSum('price', [{ price: 10 }, { price: 20 }]) // "30"

🧮 表达式能力

calc('(1 + 2) * 3') // 四则运算 + 括号
calc('max(3, 5) * 2') // 数学函数
calc(`${price} * 1.07`) // 变量用模板插值写进表达式

内置数学函数:abs sign floor ceil round trunc sqrt pow mod min max clamp(除 sqrt 和负指数 pow 按除法精度舍入外,其余精确)。

表达式是纯算术,不含变量 / 条件 / 比较 / 逻辑——变量用模板插值、条件判断在 JS 外层写a > 100 ? calc(\${a} * 0.9`) : String(a)`。

🎨 格式化(IFormat 对象)

| 字段 | 说明 | | :--- | :--- | | decimals | 小数位:number 固定、{ min, max } 区间 | | rounding | 'truncate'(默认) / 'halfUp' / 'banker' / 'ceil'(→+∞) / 'floor'(→−∞) / 'expand'(远离零) —— 含 JS 别名 trunc/round/halfEven | | thousands | true 美式 / 'eu' / 'in' | | compact | true K/M/B/T / 'zh'(万/亿)| | clamp | [min, max] 值范围限制 | | output | 'percent' / 'fraction' / 'scientific' / 'number'(或符号 %% // e num)| | plus | 显示正号 | | pad | 整数补零到 N 位 |

🧰 主要 API

| API | 作用 | | :--- | :--- | | calc(expr, options?) | 表达式求值 + 可选格式化(出错抛) | | fmt(value, options?) | calc 的展示版:支持运算 + 格式化(出错兜底) | | chainAdd/chainSub/chainMul/chainDiv | 链式运算 | | add/sub/mul/div/sqrt/pow/mod(→ number)、addStr/…/sqrtStr/powStr/modStr(→ string) | 独立运算 | | calcSum/calcAvg/calcMedian/calcMax/calcMin | 数组 / 对象数组聚合 | | setConfig/resetConfig/getConfig | 全局配置(错误兜底、默认格式、精度)|

div / divStr / chainDiv / calcAvg 末尾可传 { _precision } 单次指定除法精度,不污染全局。

更多细节见 完整文档

🌐 运行环境

构建产物语法层级为 ES2022,内部使用 BigIntArray.prototype.at,运行环境需满足:

| 环境 | 最低版本 | | :--- | :--- | | Chrome / Edge | 92 | | Firefox | 90 | | Safari(macOS / iOS)| 15.4 | | Node.js | 16.6 |

即覆盖 2022 年 3 月起的主流浏览器。其中 BigInt 是无法 polyfill 的硬性下限。

📄 License

MIT © nowo