提取小数点后 10 位数字并四舍五入至 3 位
我想实现四舍五入后四舍五入到小数点后三位的逻辑。
如果小数点 1 – 3 位的值等于 000(无论整数),并且第 4 位至第 10 位小数的值大于 0,则显示中的第 3 位小数将向上舍入。
示例:
1.1230000000 --> 1.123 1.1230010000 --> 1.123 1.1230600000 --> 1.124 1.0000010000 --> 1.001 1.0003000000 --> 1.003 5.0000001234 --> 5.001
期待
I want to implement logic of rounding up to 3 decimal positions after rounding.
If the value of decimal place 1 – 3 is equal to 000 (regardless of the whole number), and the value of the 4th thru the 10th decimal place is greater than 0, the 3rd decimal place in the display will round up.
example:
1.1230000000 --> 1.123 1.1230010000 --> 1.123 1.1230600000 --> 1.124 1.0000010000 --> 1.001 1.0003000000 --> 1.003 5.0000001234 --> 5.001
looking forward
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该可以解决正数的问题。测试您是否获得负数所需的行为(示例中未给出)。如果不是,请使用“地板”而不是“天花板”作为底片。
should do the trick for positive numbers. Test if you get the desired behavior for negative numbers (not given in your examples). If not, use Floor instead of Ceiling for negatives.
我不是一个分享点的人,但我有一般的语言经验。
不要
Math.ceil()
使用 jsInt()
或任何您拥有的int()
函数。ceil() 无论 frac 值如何!= 0 总是向上移动到下一个整数。 parseInt 砍掉了 frac 部分,这才是他真正想做的。我在 msdn 上找不到任何 sharepoint API 参考。
我基于 JavaScript/ecmascript 262 5.1 规范。
如果你想要像 5 位这样的位数,
但要注意浮点有一个问题,即使重复加或减 0.1,你也会因为浮点错误而得到疯狂的结果。要处理财务数据,您应该使用小数数据类型(如果有),如果语言提供小数数据类型,则不存在此问题。否则,您可以通过查看大整数类型并在中间或您想要的位置伪造小数点并进行相应计算来模拟定点小数。显示需要您创建自己的显示例程。但它会很快并且能力有限(没有三角函数,你必须根据整数自己编写能力)。您需要想出自己的算术例程,因为正常的算术例程假定小数点位于错误的位置。
I am not a sharepoint person, but I have general languages experience.
not
Math.ceil()
use jsInt()
or whateverint()
function you have.ceil() regardless of frac value != 0 always moves up to next integer. parseInt chops off the frac part, which is what he was really trying to do. I can't find any sharepoint API references on msdn.
I am basing this on the JavaScript/ecmascript 262 5.1 spec.
if you want number of digits like 5 digits, it's
but be aware that floating point has a problem with even repeatedly adding or subtracting 0.1 and you get wild results due to floating point error. to work with financials, you should use a decimal data type if there is one, the decimal data type if the language provides it does not have this problem. otherwise, you can simulate fixed-point fractional by looking at a large integer type and faking a decimal point in the middle or where you want it and calculating accordingly. display would require that you create your own display routines. but it would be fast and limited in abilities (no trig funcs, powers you would have to write on your own based on integers). you would need to come up with your own arithmetic routines, because normal ones assume the decimal point in the wrong place.