convert double to int

2020-01-24 07:12发布

What is the best way to convert a double to an int? Should a cast be used?

10条回答
可以哭但决不认输i
2楼-- · 2020-01-24 08:05

The best way is to simply use Convert.ToInt32. It is fast and also rounds correctly.

Why make it more complicated?

查看更多
Fickle 薄情
3楼-- · 2020-01-24 08:10

Convert.ToInt32 is the best way to convert

查看更多
来,给爷笑一个
4楼-- · 2020-01-24 08:15

Yeah, why not?

double someDouble = 12323.2;
int someInt = (int)someDouble;

Using the Convert class works well too.

int someOtherInt = Convert.ToInt32(someDouble);
查看更多
smile是对你的礼貌
5楼-- · 2020-01-24 08:15

My ways are :

 - Convert.ToInt32(double_value)
 - (int)double_value
 - Int32.Parse(double_value.ToString());
查看更多
登录 后发表回答