JavaScript 中的 BigDecimal

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16742578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:49:27  来源:igfitidea点击:

BigDecimal in JavaScript

javascriptfloating-pointbigdecimal

提问by SnakeDoc

I'm very new to JavaScript (I come from a Java background) and I am trying to do some financial calculations with small amounts of money.

我对 JavaScript 非常陌生(我来自 Java 背景),我正在尝试用少量资金进行一些财务计算。

My original go at this was:

我最初的做法是:

<script type="text/javascript">
    var normBase = ("[price]").replace("$", "");
    var salesBase = ("[saleprice]").replace("$", "");
    var base;
    if (salesBase != 0) {
        base = salesBase;
    } else {
        base = normBase;
    }
    var per5  = (base - (base * 0.05));
    var per7  = (base - (base * 0.07));
    var per10 = (base - (base * 0.10));
    var per15 = (base - (base * 0.15));
    document.write
        (
        '5% Off: $'  + (Math.ceil(per5  * 100) / 100).toFixed(2) + '<br/>' +
        '7% Off: $'  + (Math.ceil(per7  * 100) / 100).toFixed(2) + '<br/>' +
        '10% Off: $' + (Math.ceil(per10 * 100) / 100).toFixed(2) + '<br/>' +
        '15% Off: $' + (Math.ceil(per15 * 100) / 100).toFixed(2) + '<br/>'
    );
</script>

This worked well except it always rounded up (Math.ceil). Math.floorhas the same issue, and Math.roundis also no good for floats.

这很有效,只是它总是四舍五入 ( Math.ceil)。Math.floor有同样的问题,Math.round也不适合花车。

In Java, I would have avoided the use of floats completely from the get-go, however in JavaScript there does not seem to be a default inclusion of something comparable.

在 Java 中,我会从一开始就完全避免使用浮点数,但是在 JavaScript 中似乎没有默认包含类似的东西。

The problem is, all the libraries mentioned are either broken or for a different purpose. The jsfromhell.com/classes/bignumberlibrary is very close to what I need, however I'm having bizarre issues with its rounding and precision... No matter what I set the Round Type to, it seems to decide on its own. So for example, 3.7107 with precision of 2 and round type of ROUND_HALF_UPsomehow winds up as 3.72 when it should be 3.71.

问题是,所有提到的库要么坏了,要么用于不同的目的。该jsfromhell.com/classes/bignumber库非常接近我需要的东西,但是我在它的舍入和精度方面遇到了奇怪的问题......无论我将 Round Type 设置为什么,它似乎都可以自行决定。因此,例如,精度为 2 且圆形类型ROUND_HALF_UP为 3.72 的3.7107 以某种方式结束,而本应为 3.71。

I also tried @JasonSmith BigDecimal library (a machined port from Java's BigDecimal), but it seems to be for node.js which I don't have the option of running.

我也尝试过@JasonSmith BigDecimal 库(Java 的 BigDecimal 的一个机器端口),但它似乎是用于 node.js 的,我没有运行的选项。

How can I accomplish this using vanilla JavaScript (and be reliable) or is there a modern (ones mentioned above are all years old now) library that I can use that is maintained and is not broken?

我如何使用 vanilla JavaScript(并且可靠)来实现这一点,或者是否有我可以使用的现代(上面提到的那些现在已经有好几年了)库被维护并且没有损坏?

采纳答案by John Strickler

I like using accounting.jsfor number, money and currency formatting.

我喜欢accounting.js用于数字、货币和货币格式。

Homepage- https://openexchangerates.github.io/accounting.js/

主页- https://openexchangerates.github.io/accounting.js/

Github- https://github.com/openexchangerates/accounting.js

Github- https://github.com/openexchangerates/accounting.js

回答by laffuste

There are several implementations of BigDecimal in js:

BigDecimal 在js中有几种​​实现:

The last 3 come from the same author: see the differences.

最后 3 个来自同一作者:查看差异