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 🙏

© 2024 – Pkg Stats / Ryan Hefner

calc-easy

v1.1.1

Published

一个专注于易用性的、精度准确的基本运算库

Downloads

41

Readme

calc-easy

一个专注于易用性的、精度准确的基本运算库

calc-easy 是什么

calc-easy 是一个基于 big.js 的基本运算库,可以运行在大部分常见的js环境中,用法简单直观,目前支持加+、减-、乘*、除/、括号()、百分号%的混合运算,可以保持浮点数、超大数运算和四舍五入运算的精度准确。

//精度丢失现象:
0.1 + 0.2
//0.30000000000000004
19.9 * 100
//1989.9999999999998
9007199254740992 + 1
//9007199254740992
(0.355).toFixed(2)
//0.35

calc-easy 尝试解决什么问题

calc-easy 专注于解决传统的计算库在普通场景下使用时用法过于复杂、代码不够清晰直观的问题。
calc-easy 没有复杂的链式调用,支持且仅支持以字符串的形式输入数学表达式,然后直接输出字符串形式的计算结果,以达到简洁直观的目的。

let expression = '(0.1 + 0.2) * 0.3 / 1';   //数学表达式
calc(expression)
// '0.09' 

特性

  • 直接解析表达式,简单直观易用
  • 文档完整
  • 覆盖日常项目开发常用运算,支持正负整数、浮点数、超大数,计算精度准确
  • 兼容常见的浏览器和 NodeJS 环境
  • 轻量。如果现在还不够轻,将继续努力做到更轻
  • 基于 big.js,站在巨人的肩膀上,专业的事交给专业的人
  • typescript支持
  • 有单元测试

安装引入

安装:

npm install calc-easy --save

ES module:

import calc, {createCalc} from 'calc-easy';

CommonJS:

const calc = require('calc-easy');
const createCalc = calc.createCalc;
// or:
// const {createCalc} = require('calc-easy');

CDN:

<script src='https://unpkg.com/[email protected]/dist/calc-easy.min.js'></script>
var calc = calcEasy;
var createCalc = calcEasy.createCalc;

快速使用

import calc from 'calc-easy';
/* 基本使用 */
calc('1+2+3+4*5-6')
//'20'
calc('0.1 + 0.2')
//'0.3'  // 精度准确
calc('9007199254740992+1')
//'9007199254740993'  // 精度准确
calc('0.355', {toFixed: 2})
//'0.36'  // 精度准确

/* 使用变量 */
const data = {
    goodA: 12.99,
    goodB: 3.8,
    coupon: 10,
    discount: 8
};
calc('(goodA * 3 + goodB * 2 - coupon) * (discount / 10)', {
	variable: data,
	toFixed: 2
})
//'29.26'

API

calc: (expression: string, config?: config) => string

createCalc: (config) => calc

config支持两种格式:{variable?, toFixed?} 和 [variable?, toFixed?]

具体类型参见 calc-easy.min.d.ts

例子

import calc, {createCalc} from 'calc-easy';
/* 如果需要,也可以使用模板字符串传入变量 */
let data = {
	a: 1,
	b: 2,
	c: '3'
};
calc(`${data.a}-${data.b}-${data.c}`)
//'-4'

/* 使用createCalc自定义calc */
const myCalc = createCalc({
	variable: {
		Pi: 3.14
	},
	toFixed: 1
});

myCalc('1+Pi')
//'4.1'
myCalc('1+Pi+y', {
	variable: {
		Pi: 3.1416, //可以覆盖默认config执行,只影响当次。
		y: 2
	},
	toFixed: 4 //可以覆盖默认config执行,只影响当次。
})
//'6.1416' 
myCalc('Pi + x + y', [{ //数组形式的config
	x: 1,
	y: 2
}])
//'6.14'

const toFixed = createCalc({toFixed: 2});
toFixed('0.1234')
//'0.12'
toFixed('1/3')
//'0.33'

更多例子可以参考 测试用例

与其它数学计算库对比

(仅个人观点)

Licence

MIT

当前版本包大小

min:10.9KB
gzip:4.29KB

更新日志

  • 20210711(version 0.0.2):
    • 调整项目配置,压缩代码(目前min包是17.7kb,CDN版gzip压缩后是7kb
    • 支持node环境和浏览器script标签引入的形式
  • 20210717(version 0.0.4):
    • 优化配置,优化代码,减少产出体积(目前min包大小约是9.16kb,gzip压缩后约3.76kb
  • 20210731(version 0.0.5):
    • 支持百分号运算
    • 优化文档
  • 20210803(version 0.0.6):
    • 修改打包配置,修复ie11报错
  • 20210918(version 0.0.7):
    • 修复github安全报警
    • 优化readme
  • 20220304(version 0.0.8):
    • 支持大于9007199254740992的大数
    • 修复正号(1*+2)导致的报错
    • 引入jest单元测试
  • 20220310(version 1.0.0):
    • config支持json和数组两种格式
    • 加入createCalc方法
    • 重写文档
    • 完善测试
  • 20220704(version 1.1.1):
    • 优化变量替换的正则,修复问题