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

node-floatobj

v1.0.2

Published

计算中浮点数精度

Downloads

1

Readme

安装

use npm install

使用npm安装

npm install node-floatobj

download form GitHub

从github下载

copy code

复制以下代码

var floatObj = function() {

    function dealNumber(num,digits){
        // var num2=num.toFixed(digits+1);
        // return  parseFloat(num2.substring(0,num2.lastIndexOf('.')+(digits+1)));
        var num2=num.toFixed(digits+2);
        //获取2位数的数字
        num2=parseFloat(num2.substring(0,num2.lastIndexOf('.')+(digits+1)));
        //弥补精度问题,乘以100,加上0.5
        num2 = parseInt(num2*100 + (num2>0?0.5:-0.5))/100;

        return num2;
    };

    function getFloatObj(num,digits){
        var str = num.toString();
        var len = str.length;
        var index = str.lastIndexOf(".");
        //计算出小数位
        var times = Math.pow(10,index>-1?len-(index+1):0)
        return {
            num:dealNumber(num * times,digits),
            times:times
        }
    }
 
    /*
     * 核心方法,实现加减乘除运算,确保不丢失精度
     * 思路:把小数放大为整数(乘),进行算术运算,再缩小为小数(除)
     *
     * @param a {number} 运算数1
     * @param b {number} 运算数2
     * @param digits {number} 精度,保留的小数点数,比如 2, 即保留为两位小数
     * @param op {string} 运算类型,有加减乘除(add/subtract/multiply/divide)
     *
     */
    function operation(a, b, digits, op) {
        // console.log(a,b);
        var o1 = getFloatObj(a,digits)
        var o2 = getFloatObj(b,digits)
        // console.log(o1,o2);
        var n1 = o1.num
        var n2 = o2.num
        var t1 = o1.times
        var t2 = o2.times
        // console.log(n1,n2,t1,t2);
        var max = t1 > t2 ? t1 : t2
        var result = null
        switch (op) {
            case 'add':
                if (t1 === t2) { // 两个小数位数相同
                    result = n1 + n2
                } else if (t1 > t2) { // o1 小数位 大于 o2
                    result = n1 + n2 * (t1 / t2)
                } else { // o1 小数位 小于 o2
                    result = n1 * (t2 / t1) + n2
                }
                // console.log(result);
                return dealNumber(result / max, digits);
            case 'subtract':
                if (t1 === t2) {
                    result = n1 - n2
                } else if (t1 > t2) {
                    result = n1 - n2 * (t1 / t2)
                } else {
                    result = n1 * (t2 / t1) - n2
                }
                return dealNumber(result / max, digits);
            case 'multiply':
                result = (n1 * n2) / (t1 * t2)
                return dealNumber(result, digits);
            case 'divide':
                if (t1 === t2) { // 两个小数位数相同
                    result = n1 / n2
                } else if (t1 > t2) { // o1 小数位 大于 o2
                    result = n1 / (n2 * (t1 / t2))
                } else { // o1 小数位 小于 o2
                    result = (n1 * (t2 / t1)) / n2
                }
                return dealNumber(result, digits);
        }
    }
 
    // 加减乘除的四个接口,digits精度,保留几位小数
    function add(a, b, digits) {
        return operation(a, b, digits || 2, 'add')
    }
    function subtract(a, b, digits) {
        return operation(a, b, digits || 2, 'subtract')
    }
    function multiply(a, b, digits) {
        return operation(a, b, digits || 2, 'multiply')
    }
    function divide(a, b, digits) {
        return operation(a, b, digits || 2, 'divide')
    }
 
    // exports
    return {
        add: add,
        subtract: subtract,
        multiply: multiply,
        divide: divide
    }
};

//直接初始化使用
var floatObj = floatObj();

Introduce 模块说明

改模块主要用于处理计算过程中的浮点数精度问题,对四则运算进行的一定程度的封装

Sample example 使用说明

var floatObj = require("node-floatobj")

/**
 * 加法 add(num1,num2,digits)
 * 减法 subtract(num1,num2,digits)
 * 乘法 multiply(num1,num2,digits)
 * 除法 divide(num1,num2,digits)
 *
 * num1,num2为数字,digits为精度,默认为2位数
 */
console.log(floatObj.add(1099.891,100.1));
console.log(floatObj.subtract(1099.891,100.1));
console.log(floatObj.multiply(1099.89,1000));
console.log(floatObj.divide(1099.89,100));

console.log(1098.89/100);