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

@likg/big

v1.0.1

Published

处理JavaScript浮点数计算精度的工具

Downloads

1

Readme

概述

由于JavaScript是以64位的浮点数形式存储的,所以在计算过程中会存在较大差异,比如:

// 加法
0.1 + 0.2 = 0.30000000000000004

// 减法
1.5 - 1.2 = 0.30000000000000004
0.3 - 0.2 = 0.09999999999999998

// 乘法
19.9 * 100 = 1989.9999999999998
19.9 * 10 * 10 = 1990
1306377.64 * 100 = 130637763.99999999
1306377.64 * 10 * 10 = 130637763.99999999
0.7 * 180 = 125.99999999999999
9.7 * 100 = 969.9999999999999
39.7 * 100 = 3970.0000000000005

// 除法
0.3 / 0.1 = 2.9999999999999996

这对于数字敏感型项目是非常致命的,目前有很多处理浮点数计算的库,比如 bigNumber.jsbig.js。为了深入理解其处理方式,这里自己写了一个lg-big库便于自己在项目中使用,分享给大家。

使用

  1. 加法运算

    let a = new Big(0.1);
    let b = new Big(0.2);
    console.log(a.plus(b).parse()); // 3
    console.log(a.plus(0.9).parse()); // 1
  2. 减法运算

    let a = new Big(10);
    let b = new Big(0.1);
    console.log(a.minus(b).parse()); // 9.9
    console.log(a.minus(1).parse()); // 9
  3. 乘法运算

    let a = new Big(10.9);
    let b = new Big(100);
    console.log(a.multipliedBy(b).parse()); // 1090
  4. 除法运算

    let a = new Big(0.3);
    let b = new Big(0.1);
    console.log(a.dividedBy(b).parse()); // 3
  5. 固定小数点位数,默认2位

    let a = new Big(0.3);
    console.log(a.plus(0.1).digits(5)); // 0.40000
    console.log(Big.digits(31)); //  31.00
    console.log(Big.digits(31, 5)); // 31.00000
    console.log(Big.digits(3.1415926, 4)); // 3.1415
    console.log(Big.digits(3.14, 4)); // 3.1400
  6. 人民币格式处理

    let a = new Big(88.6);
    console.log(a.rmb()); // 88.60
    console.log(a.plus(0.02).rmb()); // 88.62
    
    console.log(Big.rmb(88)); // 88
    console.log(Big.rmb(88.6)); // 88.60
    console.log(Big.rmb(88.69)); // 88.69
    console.log(Big.rmb(88.69623)); // 88.69
  7. 切割数字,使用场景:UI在设计价格展示时,整数部分和小数部分样式不一致需单独处理时

    console.log(new Big(2).plus(1.2).split()); // ['3', '20']
    console.log(Big.split(3.14)); // ['3', '14']
    console.log(Big.split(12.34)); // ['12', '34']
  8. 省略,如果超过1万,则返回万制,转换后小数点后保留两位

    console.log(new Big(9999).plus(1000).ellipsis()); // 1.09万
    console.log(Big.ellipsis(13210)); // 1.32万