如何在C#中.50之后舍入十进制值? [英] How to round decimal value after .50 in C#?
本文介绍了如何在C#中.50之后舍入十进制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想像这样绕小数值
如果5.50 => 5
如果5.51 => 6 ..
我想如果点getter之后的十进制值比.50然后值增加1.
我使用了Math.Round()函数,但它是圆的在.49十进制值之后..
I want to round the decimal value like this
if 5.50 => 5
if 5.51 => 6 ..
I want if the decimal value after point getter than .50 then the value increment by 1.
I used Math.Round() function, but it's round after .49 decimal value..
推荐答案
使用自定义舍入系统:
Use a custom rounding system:
public int Round(double value)
{
double decimalpoints = Math.Abs(value - Math.Floor(value));
if (decimalpoints > 0.5)
return (int)Math.Round(value);
else
return (int)Math.Floor(value);
}
希望这会有所帮助。 : - )
Hope this helps. :-)
public int myRound(double val)
{
return (int)Math.Floor(val + .49);
}
我不喜欢魔术数字所以你可能想要定义一个const double并将它换成0.49
I don't like "magic numbers" so you might want to define a const double and swap it for the 0.49
基本和简单的方法,你可以将十进制(5.50)转换为int(5),你可以检查5.50-5大于或小于.50如果更大然后应用你的算法..但我不知道任何其他函数自动完成。
basic and simple way , you can convert decimal (5.50) to int (5) and you can check for 5.50-5 greater or lower than .50 if bigger then apply your algorithim..but i dont know any other function does that automaticly.
这篇关于如何在C#中.50之后舍入十进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文