以 10 为基数四舍五入
我有一个函数可以将给定函数的数字四舍五入到最接近的整便士。
<script type='text/javascript'>
Math.roundNumber = function(a,b){
if(!isNaN(parseInt(a))){
c = Math.pow(10,b);
return (Math.round(a*c)/c);
}
return false;
}
</script>
然而,我注意到输入到该函数中的所述数字只能四舍五入到最接近的一便士,但必须四舍五入到小数点后两位。
EG
15.236562213541684 would = 15.24 9846.65456169846 would = 9846.66
我以为这只是一个改变的情况 return (Math.round(ac)/c); 到 return (Math.ceil(ac)/c);
显然我错了。
对这个问题有什么帮助吗?
** 编辑 **
这是我试图实现的公式,也许它会有所帮助
a = intrest price
b = terms
c = a/b
d = c*(b-1)
e = a-d
,例如,
a = 295.30
b = 156
c = 295.30/156 = 1.90 (rounded up to nearest decimal as above)
d = 1.90 * (b-1) = 294.50
e = 295.30 - 294.50 = 0.80
任何人都可以纠正一个函数来执行上述操作吗?
这里是我当前代码的链接,包括公式...它是我制作的一个非常旧的公式当我第一次开始使用 JavaScript 时(已经是不久前的事了),但我现在仍然没有像那时那样好转。
任何人都可以清理它,看看他们是否能明白为什么它不能与上面的函数匹配?
I have a function to round a number given to the function to the nearest whole pence.
<script type='text/javascript'>
Math.roundNumber = function(a,b){
if(!isNaN(parseInt(a))){
c = Math.pow(10,b);
return (Math.round(a*c)/c);
}
return false;
}
</script>
however it has come to my attention that the said number inputted into the function must only be rounded up to the nearest one pence however it must be rounded to two decimal places.
E.G.
15.236562213541684 would = 15.24 9846.65456169846 would = 9846.66
I thought it would just be a case of changing
return (Math.round(ac)/c);
To
return (Math.ceil(ac)/c);
Obviously I was very wrong.
Any help on this matter?
** EDIT **
Here is the formula that I'm trying to achieve maybe it'll help
a = intrest price
b = terms
c = a/b
d = c*(b-1)
e = a-d
so for example
a = 295.30
b = 156
c = 295.30/156 = 1.90 (rounded up to nearest decimal as above)
d = 1.90 * (b-1) = 294.50
e = 295.30 - 294.50 = 0.80
can anyone right a function to do the above?
Here is a link to the current code i have including the formula... its a very old formula that I made when I first started JavaScript (which is a while ago now) however I'm still no better now as I was back then.
Can anyone clean it up to see if they can see why its not working to match the function above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
已经有一个内置函数可以实现此目的,
Number.toFixed (numDigits)
,为什么不使用这个呢?示例:
最后一个与您的示例不匹配 -
9846.65456169846
四舍五入到小数点后两位应为9846.65
,而不是9846.66
。There's a built-in function for this already,
Number.toFixed(numDigits)
, why not use this?Example:
This last one doesn't match your example -
9846.65456169846
rounded to two decimal places should be9846.65
, not9846.66
.您的代码比此任务所需的更复杂。如果您知道小数点始终代表稳定的值,无论小数点两侧的数字长度如何,那么您很幸运。
1) 那么您只需将原始数字乘以 100
2) if (Number(x.charAt(indexOF(".") + 1)) > 4) {x += 1} where x is your number
3) parseInt
4)除以100就完成了。
Your code is more complex than what you need for this task. If you know the decimal point will always represent a stable value regardless of the length of digits on either side of that decimal point you are in luck.
1) Then you must merely multiple your original number by 100
2) if (Number(x.charAt(indexOF(".") + 1)) > 4) {x += 1} where x is your number
3) parseInt
4) divide by 100 and you are done.
使用定点运算的建议是个好建议。不幸的是,Javascript 中没有定点算术 - 所有数字都是双精度浮点数。 (好吧,如果不引入一些显式支持库,就没有定点算术。)
为了清理文本字段中的输入,我已经开始将数字作为字符串处理(在任何可能的范围内)。将输入分成小数点左右的部分,然后处理整数值(必要时通过 Math.floor() )。如果您必须处理小数点后第二位之后的杂散数字,要么将它们视为错误(它们到底意味着什么?),要么再次将它们分开并作为整数使用。
如果您不处理用户提供的值(即来自文本输入字段的内容),那么您确实不应该在 Javascript 中执行此操作,因为同样,您没有您真正需要的数字工具。
Advice to use fixed-point arithmetic is good advice. Unfortunately, there is no fixed-point arithmetic in Javascript - all numbers are double-precision floating-point numbers. (Well, no fixed-point arithmetic without pulling in some explicit support library.)
For scrubbing input in text fields, I've started dealing with the numbers as strings (to whatever extent possible). Chop the input up into the parts to the left and right of the decimal point, then work in integer values (via Math.floor() when necessary). If you've got to deal with stray digits past the 2nd decimal place, either treat them as an error (what do they mean anyway?) or else, again, split them off and work with them as integers.
If you're not dealing with user-supplied values (i.e., stuff from text input fields), then you really shouldn't be doing this in Javascript, because, again, you don't have the numeric tools you really need.
将
Math.round()
替换为Math.ceil()
应该可以:该错误一定在其他地方。另外,我建议完全删除
isNaN(parseInt())
,因为数学函数无论如何都会转换为数字,如果不可能,则返回NaN
。这将导致函数也返回 NaN,这比布尔值 false 更合适。顺便说一句:将浮点数学与货币值一起使用是一个坏主意:改用定点算术!
现在我们知道您要做什么,下面是计算示例所需值的代码:
将所有计算的
round()
更改为ceil()
并没有不起作用,因为最后一次付款必须正常舍入,以补偿由于使用浮点值而导致的不准确性。通常最好只使用整数数学,因此将所有值乘以 100 并仅在输出值时进行转换:
Replacing
Math.round()
withMath.ceil()
should work: The bug must be somewhere else.Also, I suggest dropping the
isNaN(parseInt())
completely as the math functions will cast to number anyway, returningNaN
if that's not possible. This will lead to the function returningNaN
as well, which is a better fit than booleanfalse
.And btw: it's a bad idea to use floating point math with monetary values: use fixed point arithmetics instead!
Now that we know what you're trying to do, here's the code which computes the desired values for your example:
Changing
round()
toceil()
for all computations didn't work as the last payment has to be rounded normally to compensate the inaccuracies due to using floating point values.It's generally better to only use integer math, so multiply all values with
100
and convert only when outputting the values:在这种情况下我得到了你想要的结果。不确定它是否每次都会起作用,但似乎是。我肯定会先检查它是否按照您的要求工作。
I got it to give the results you want in this case. Not sure if it's going to work every time but it seems to. I'd definitely check it works as you want it to first.
不,你是对的。
我用 ceil 修改测试了你的代码,它有效。
No, you were right.
I tested your code with the ceil modifictaion, and it works.