how to convert float to int in c#?
but without converting to string and then to int
Convert float to int
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Solitaire85 reputationPosted Aug 17, 2005, 5:52 PM EDT
If you need to convert a larger type into a smaller type, such as a Double to an Integer, an implicit conversion would not work. In order to convert one type to another, you need to use explicit conversion. But be careful. If the value of the larger variable type is greater than it would fit into the smaller variable type then it will result in an overflow error. For example, to convert a Double to an Integer, the magnitude of the Double value must be within the range of acceptable values for the Integer data type
C# Casting: In C#, explicit conversions take place when you "cast" a value to a different type You can force a larger type to convert to a smaller type. You must explicitly convert using the cast operator. Examples:
The primitive type to convert to is enclosed within parentheses, followed by the variable to convert. If the value in the larger type is greater than the range in the smaller type, it will convert but the result will be incorrect. If a floating type contains a fraction and is cast to an integer type, it will be rounded out.