要舍入到下一个10位的数字 [英] Number to be rounded to next 10 digit

查看:105
本文介绍了要舍入到下一个10位的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,像17.3这样的数字被舍入到20.0。

或者假设数字是11.5也被舍入到20.0

或者假设数字是10.3也被舍入到10.0

解决方案

而不是舍入,考虑使用所需舍入的数值的字符串格式。这不完全是一回事。纯粹的显式舍入可能是危险的,因为由于某种原因,人们可能会尝试使用舍入值并最终导致精度损失。在需要这种舍入的情况下很少(例如,在随机数生成器中)。通常,当您在scree上显示它时,您只需要舍入值,或类似的东西。所以,你可以使用格式化。例如,对于类型 System.Double ,您将需要: http://msdn.microsoft.com/en-us/library/system.double.tostring%28v=vs.110%29.aspx [ ^ ]。



格式说明符请参阅:

http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx [ ^ ],

http:// msdn .microsoft.com / zh-CN / library / 0c899ak8(v = vs.110).aspx [ ^ ]。



如果您不想全部学习,可以使用格式化实用程序 https://code.msdn.microsoft.com/NET-Framework-4-Formatting-9c4dae8d [ ^ ]。



-SA

For example, a number like 17.3 is being rounded to 20.0.
OR suppose the number is 11.5 is being also rounded to 20.0
OR suppose the number is 10.3 is being also rounded to 10.0

解决方案

Instead of rounding, think about string formatting of numeric values with desired rounding. This is not exactly the same thing. Pure explicit rounding can be dangerous, because one could, by some reason, try to use rounded value and end up with loss of precision. There are very few cases when such rounding is needed (in random number generators, for example). Normally, you only need rounded value when you show it on scree, or something like that. So, you can just use formatting. For the type System.Double, for example, you will need this: http://msdn.microsoft.com/en-us/library/system.double.tostring%28v=vs.110%29.aspx[^].

For format specifiers, please see:
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx[^].

If you don't want to learn it all, you can use the formatting utility: https://code.msdn.microsoft.com/NET-Framework-4-Formatting-9c4dae8d[^].

—SA


这篇关于要舍入到下一个10位的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆