javascript 数学,以 0.05 为增量四舍五入到小数点后两位
我提前道歉。我读过很多帖子,我确信答案在于实现 toFixed() 方法,但不知道如何实现。
$('.addsurcharge').click(function() {
$('span.depositamount').text(function(i,v) {
return Math.round(parseInt(v * 100, 10) * 0.025) / 100 + (v * 1);
});
});
$('.nosurcharge').click(function() {
$('span.depositamount').text(function(i,v) {
return Math.round(parseInt(v * 100, 10) * -0.025) / 100 + (v * 1);
});
});
基本上,上述内容是为 AMEX 存款添加(或减去)2.5% 的附加费,我希望它四舍五入到最接近的小数点后两位。最好以 0.05 为增量。
帮助表示感谢!
I apologise in advance. I've read thru a number of post and I'm sure the answer lies in implementing the toFixed()
method, but can't work out how.
$('.addsurcharge').click(function() {
$('span.depositamount').text(function(i,v) {
return Math.round(parseInt(v * 100, 10) * 0.025) / 100 + (v * 1);
});
});
$('.nosurcharge').click(function() {
$('span.depositamount').text(function(i,v) {
return Math.round(parseInt(v * 100, 10) * -0.025) / 100 + (v * 1);
});
});
Basically, the above is adding (or subtracting) a 2.5% surcharge for AMEX deposits and I want it to round up to the nearest two decimal amount. Preferably in .05 increments.
Help appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
货币最简单的方法是使用小单位,然后四舍五入并仅在最后转换为主要单位。因此,对于包含美元和美分的系统,请以美分计算直到最后,然后转换为美元。如果要输入的文本格式为“2.03”,那么它可以使用以下内容将其四舍五入到最接近的 $0.05:
这可以更简洁,但我看不到这一点,它只会导致混淆。字符串操作非常快,用简单的加法和减法取代了 JavaScript 小数乘法和除法的不确定性。
例如借用 rfausak 的答案:
The easiest way for currency is to work in the minor units, then round and convert to major units only at the end. So for a system with dollars and cents, work in cents until the end, then convert to dollars. If the text to be input is of the format "2.03", then it can rounded it to the nearest $0.05 using something like:
That can be more concise, but I can't see the point, it would only server to obfuscate. String manipulation is very fast and replaces the uncertainty of javascript multiplication and division of decimal fractions with simple addition and subtraction.
e.g. borrowing from rfausak's answer:
如果您的问题是“我如何采用
v
(包含美元和美分金额的变量)并应用 2.5% AMEX 附加费,并将结果四舍五入到最接近的美分(即,四舍五入到两分)小数)?”然后您可以尝试以下操作:如果您有可变附加费,请尝试以下操作:
请注意,
toFixed
返回字符串表示形式,但它会为您进行舍入。(关于您的“无附加费”功能,您无法通过乘以 -0.025 来删除之前应用的 0.025 附加费。您通过乘以 1.025 来应用附加费,因此您可以通过除以 1.025 来删除它。)
If your question is "How do I take
v
, a variable that contains a dollar-and-cents amount, and apply a 2.5% AMEX surcharge with the result rounded to the nearest cent (i.e., to two decimals)?" Then you can try this:If you have a variable surcharge try this:
Note that
toFixed
returns a string representation, but it does the rounding for you.(Regarding your "nosurcharge" function, you can't remove a 0.025 surcharge that was applied previously by multiplying by -0.025. You apply the surchage by multiplying by 1.025, so you remove it by dividing by 1.025.)
虽然帖子有点旧,但问题不是。
我认为海报的意思是,某些货币以美分计算,但价格以 x.00 或 x.05 美分给出。例如瑞士货币。
解决方案借鉴了 RobG 的答案,并做了一些修改:
Although the post is a bit old, the question isn't.
I think what the poster meant was, that some currency calculate with cents, but the prices are given in x.00 or x.05 cents. For example Swiss currency.
The solution, borrowed from RobG's answer with a little fix:
下面的 JavaScript 怎么样:
How about the following javascript:
这个简单的函数非常适合舍入到 x 位。
This simple function works well for rounding to x number of places.