如何舍入小数 [英] How to round decimal

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

问题描述



我想向下舍入,向上舍入十进制数。

例如,12.9879 - > 12.97 - 向下舍入

12.9879 ---> 12.99 - 向上舍入

Hi,
I want to round down,round up the decimal number.
For example,12.9879-->12.97--round down
12.9879--->12.99--round up

推荐答案

我怀疑你真的需要四舍五入。它几乎从不需要。有可能,您只需要以圆形形式在屏幕上显示该值。这是不同的,因为你不要直接舍入,只使用正确的字符串格式。请参阅:

http://msdn.microsoft.com/en-us /library/fzeeb5cd.aspx [ ^ ],

http://msdn.microsoft.com/en -us / library / dwhawy9k.aspx [ ^ ],

http://msdn.microsoft.com /en-us/library/0c899ak8.aspx [ ^ ]。



注意,这也比舍入更安全:您保证舍入不会潜入计算并且不会导致精度损失。



-SA
I doubt you really need rounding per se. It is almost never needed. Chances are, you only need to present the value on screen in a rounded form. And this is something different, as you don''t do rounding directly and only use proper string formatting. Please see:
http://msdn.microsoft.com/en-us/library/fzeeb5cd.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

Note that is is also safer then rounding: you guarantee that rounding does not sneak into calculations and does not cause precision loss.

—SA


首先你问题的舍入逻辑似乎是错误的...... 12.9879应该向下舍入到12.98如果你想让它向下舍入。



然而,它花了我大约2秒的时间谷歌找到这个链接:



http://stackoverflow.com/questions/2632753/rounding-values-up-or-down-in-c-sharp [ ^ ]
Firstly your rounding logic in your question appears to be wrong.... 12.9879 should round down to 12.98 if you want it to round down.

However, it took me all of about 2 seconds with google to find this link:

http://stackoverflow.com/questions/2632753/rounding-values-up-or-down-in-c-sharp[^]


public static decimal Round(

十进制d,

int十进制数





其中d - 要舍入的十进制数和小数 - 返回值中的小数位数。
public static decimal Round(
decimal d,
int decimals
)

where d - A decimal number to be rounded, and decimals - The number of decimal places in the return value.


这篇关于如何舍入小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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