C#中double型如何转成int型,小数部分四舍五入到整数? 例如3.3在转成整数时为3,-3.8在转成整数时为-4。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 试加正负0.5一下 Console.WriteLine((int)(3.3 + 0.5)); Console.WriteLine((int)(-3.8 - 0.5)); Math.Floor(3.8)//返回小于或等于指定数字的最大整数,结果为3Math.Round(3.3)//将值舍入到最接近的整数或指定的小数位数,结果为4 不好意思,写错了,更改如下:Math.Floor(3.8)//返回小于或等于指定数字的最大整数,结果为3Math.Round(3.8)//将值舍入到最接近的整数或指定的小数位数,结果为4 Round肯定是不对的 double x = -3.8; int y = 0; if (x != 0) y = x > 0 ? (int)(x + 0.5) : (int)(x - 0.5); Console.WriteLine(y); Math.Round系统已经考虑正负数了。 请问Convert.ToInt32(-3.8)等于几?Convert.ToInt32(3.3)等于几? Math.Round实现的是banker舍入法http://hi.baidu.com/frwl/blog/item/eb64c39537ab7f4fd0135e20.html 不能直接调用Math.Round方法的,这可和Java的不一样哦Math.Round这个函数的解释是将值按指定的小数位数舍入,并不就是四舍五入。这种舍入有时称为就近舍入或四舍六入五成双Math.Round(0.4) //result:0 Math.Round(0.6) //result:1 Math.Round(0.5) //result:0 Math.Round(1.5) //result:2 Math.Round(2.5) //result:2 Math.Round(3.5) //result:4 Math.Round(5.5) //result:6 Math.Round(6.5) //result:6 Math.Round(8.5) //result:8 Math.Round(9.5) //result:10可以看出 并不是四舍五入的 其实在 VB, VBScript, C#, J#, T-SQL 中 Round 函数都是采用 Banker's rounding(银行家舍入)算法,即四舍六入五取偶。事实上这也是 IEEE 规定的舍入标准。因此所有符合 IEEE 标准的语言都应该是采用这一算法的。请调用 Math.Round(Decimal, MidpointRounding) 重载!~哦,原来还有重载的方法可用,MidpointRounding在两个数字之间时如何舍入的规范,规范MidpointRounding中它有2个成员,一个是ToEven还有个是AwayFromZero。//四舍五入 Math.Round(0.5,MidpointRounding.AwayFromZero) 以前我也碰到这个问题 虽然知道了Math.Round(0.5,MidpointRounding.AwayFromZero)来解决,但是MidpointRounding.AwayFromZero里面是如何算的 我还没搞清楚 我还写了一片文章总结下 但MidpointRounding.AwayFromZero的MSDN中的解释令我很难理解(没空去研究了),结果写了一半就搁置在哪了 兄弟是否能去研究下看看http://blog.csdn.net/chopper7278/archive/2008/12/02/3429388.aspx 岂敢, wuyazhe兄 能不能帮我去看看msdn对MidpointRounding.AwayFromZero的解释 我实在不解阿... 现在主要都没时间去看了 拿出来大家讨论下看看msdn对AwayFromZero的解释: 当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。 这个...想不出来了哦... double a = 2.3;double b = 4.5;Response.Write("a:"+a.ToString("0")+"<br>");Response.Write("b:"+b.ToString("0"));------------a:2b:5 这叫银行家舍入法...可看一下我在近3年前写的老文章...http://blog.csdn.net/vrhero/archive/2007/07/29/1714889.aspx 这句话你想不出来很正常,因为这是MSDN中文译版的一个错误,始终没改正... 对了,还要补充一下...这个MidpointRounding.AwayFromZero枚举现在已经变成四舍五入算法了,那个解释已经不通了... Convert.ToInt32(-3.8)这是很万能的转换! 都说成这样了。。还没找到答案?//四舍五入 Math.Round(0.5,MidpointRounding.AwayFromZero)这样就可以了 Convert.ToInt32 采用的是四舍六入五成双的方法处理小数的,所以某些时候会有问题的。Convert.ToInt32(3.5); //4Convert.ToInt32(-3.5); //-4Convert.ToInt32(4.5); //4Convert.ToInt32(-4.5); //-4 Math.Round(0.5,MidpointRounding.AwayFromZero) 貌似正解... Word.Shape 右键 复制 字符串转义 关于字符串截取的问题( 刚刚发的那个没表达清楚,引起大家误会了不好意思 解决马上给分) 急急~~~~~ 模式窗口变白 用c#有没有方法自动退出U盘 高手救命!! [通信难题]WCF通信难题 一个小问题,在.NET里获得文件名不支持中文。。。 听说MS不赞成c#开发游戏,请教一下。 SubmitBtn_Click(Object sender, EventArgs e) 关于这个转换xml文件 菜鸟提问,C#继承的问题。
Console.WriteLine((int)(3.3 + 0.5));
Console.WriteLine((int)(-3.8 - 0.5));
Math.Round(3.3)//将值舍入到最接近的整数或指定的小数位数,结果为4
Math.Floor(3.8)//返回小于或等于指定数字的最大整数,结果为3
Math.Round(3.8)//将值舍入到最接近的整数或指定的小数位数,结果为4
Round肯定是不对的
double x = -3.8;
int y = 0;
if (x != 0)
y = x > 0 ? (int)(x + 0.5) : (int)(x - 0.5);
Console.WriteLine(y);
系统已经考虑正负数了。
Convert.ToInt32(3.3)等于几?
Math.Round(0.6) //result:1
Math.Round(0.5) //result:0
Math.Round(1.5) //result:2
Math.Round(2.5) //result:2
Math.Round(3.5) //result:4
Math.Round(5.5) //result:6
Math.Round(6.5) //result:6
Math.Round(8.5) //result:8
Math.Round(9.5) //result:10可以看出 并不是四舍五入的
其实在 VB, VBScript, C#, J#, T-SQL 中 Round 函数都是采用 Banker's rounding(银行家舍入)算法,即四舍六入五取偶。事实上这也是 IEEE 规定的舍入标准。因此所有符合 IEEE 标准的语言都应该是采用这一算法的。请调用 Math.Round(Decimal, MidpointRounding) 重载!~哦,原来还有重载的方法可用,MidpointRounding在两个数字之间时如何舍入的规范,规范MidpointRounding中它有2个成员,一个是ToEven还有个是AwayFromZero。//四舍五入
Math.Round(0.5,MidpointRounding.AwayFromZero)
http://blog.csdn.net/chopper7278/archive/2008/12/02/3429388.aspx
这个...想不出来了哦...
double a = 2.3;
double b = 4.5;
Response.Write("a:"+a.ToString("0")+"<br>");
Response.Write("b:"+b.ToString("0"));------------
a:2
b:5
都说成这样了。。还没找到答案?//四舍五入
Math.Round(0.5,MidpointRounding.AwayFromZero)这样就可以了
Convert.ToInt32 采用的是四舍六入五成双的方法处理小数的,所以某些时候会有问题的。
Convert.ToInt32(3.5); //4
Convert.ToInt32(-3.5); //-4
Convert.ToInt32(4.5); //4
Convert.ToInt32(-4.5); //-4