C# - Converting a float to an int… and changing th

2020-03-01 03:16发布

This is probably a really newbie question (well, I'm pretty sure it is), but I have a float that's being returned and I need a quick and efficient way of turning it into an int.

Pretty simple, but I have an exception. If the remainder of the float is anything other than .0 then I want to increment the int.

Some quick examples:

Float = 98.0, Int = 98
Float = 98.1, Int = 99
Float = 6.6, Int = 7
etc.

3条回答
够拽才男人
2楼-- · 2020-03-01 03:32

This should do it:

int myInt = (int)Math.Ceiling(myFloat);
查看更多
趁早两清
3楼-- · 2020-03-01 03:50
Convert.ToInt32(Math.Ceiling(FloatValue));
查看更多
够拽才男人
4楼-- · 2020-03-01 03:51

Use

Math.Ceiling();

as Math.Round() won't make 98.1 equal to 99

查看更多
登录 后发表回答