29

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 Answers 3

61

This should do it:

int myInt = (int)Math.Ceiling(myFloat);
Sign up to request clarification or add additional context in comments.

Comments

11

Use

Math.Ceiling();

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

Comments

4
Convert.ToInt32(Math.Ceiling(FloatValue));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.