JavaScript中的BigDecimal [英] BigDecimal in JavaScript
问题描述
我原来是JavaScript的新手(我来自Java背景),我正在尝试用少量的资金进行一些财务计算。去这是:
< 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)+'<>'+
'15'Off:$'+(Math.ceil(per15 * 100) / 100).toFixed(2)+'< br />'
);
< / script>
除了总是四舍五入( Math.ceil
)。 Math.floor
具有相同的问题,并且 Math.round
对浮点数也是没有好处的。
在Java中,我将避免从开始就完全使用浮点数,但是在JavaScript中似乎没有默认的可比较的东西(OMG WHY!?!?所以,我的SO-fu让我看到这个帖子: 是一个很好的JavaScript的bigdecimal库> https://stackoverflow.com/questions/744099/is-there-a-good-javascript-bigdecimal-library
问题是,所提及的所有库都被破坏或用于不同的目的。 jsfromhell.com/classes/bignumber
图书馆非常接近我所需要的,但是我有四舍五入的问题和精确度...无论我设置了什么圆型到,似乎是自行决定的。因此,例如,精度为2的3.7107和 ROUND_HALF_UP
的圆形类型应该是3.71,但应该是3.71。
我也试过了@JasonSmith BigDecimal库(从Java的BigDecimal中加工过的一个端口),但它似乎是node.js,我没有选择运行。
如何使用vanilla JavaScript(而且是可靠的)或者是否有现代的(上面提到的都是现在的岁数)库我可以用这个是保持和不被破坏?
我喜欢使用 accounting.js
数字,货币和货币格式。
首页 - https://openexchangerates.github.io/accounting.js/
Github - https://github.com/openexchangerates/accounting.js
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.
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.floor
has the same issue, and Math.round
is also no good for floats.
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 (OMG WHY!?!?!?!).
So, my SO-fu has led me to this post: https://stackoverflow.com/questions/744099/is-there-a-good-javascript-bigdecimal-library
The problem is, all the libraries mentioned are either broken or for a different purpose. The jsfromhell.com/classes/bignumber
library 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_UP
somehow winds up as 3.72 when it should be 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.
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?
I like using accounting.js
for number, money and currency formatting.
Homepage - https://openexchangerates.github.io/accounting.js/
Github - https://github.com/openexchangerates/accounting.js
这篇关于JavaScript中的BigDecimal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!