【提问】
C#需要将双精度值转换为int类型。
【解决流程】
1 .参考:
尝试了Math的Floor和Ceiling。 已经是Convert.Int64和Int32。 测试结果为代码: private void doubleToIntTest (
{
doubletimestampdouble=CRL.getcurtimeinmillisec (; //1343403554609.3293
int 64 timestamp int 64=convert.toint 64 (timestamp double ); //1343403554609
//anunhandledexceptionoftype ' system.overflow exception ' occurredinmscorlib.dll
//additional information 3360 valuewaseithertoolargeortoosmallforanint 32。
//int 32 timestamp int 32=convert.toint 32 (timestamp double );
int timeStampInt=(int ) timeStampDouble; //-2147483648
doubledoublefllor=math.floor (timestamp double; //1343403554609.0
doubledoubleceiling=math.ceiling (timestamp double; //1343403554610.0
doubledoubleround=math.round (timestamp double; //1343403554609.0
int 64 FLL orint 64=convert.toint 64 (doublefllor ); //1343403554609
int 64 Ceiling int 64=convert.toint 64 (双Ceiling; //1343403554610
int 64 round int 64=convert.toint 64 (double round ); //1343403554609
}
【总结】
1 .要将c #的双精度变量转换为int变量,请执行以下操作:
如果双精度值不大,可以使用Convert.ToInt32或直接(int )强制转换,但如果强制转换,即使发生错误也不会报告错误,因此在发生数据溢出等问题时
如果双精度数值超过int32,请使用Conver.ToInt64。
2 .注意如果想四舍五入相应的小数,直接通过Math.Round即可,但得到的结果仍然是双精度的。
附件: