Convert.ToInt32()
converts a floating-point value (float, double) to a 32-bit integer value. We pass a floating-point value to this function, and it will output an Int32
type value.
Convert.ToInt32(floatValue).
This method only takes a single parameter:
floatValue
: This is the floating-point value that we want to convert to a 32-bit integer.It returns an Int32
value corresponding to the floatValue
. For example, if floatValue
is 21.34, it will return 21.
Let's look at the code below:
using System;class HelloWorld{static void Main(){// create some floating point valuesfloat floatVal = 3.0001F;double doubleVal = 53.223;// convert to Int32 and print resultsConsole.WriteLine("Float value: {0}, Int32 value: {1}", floatVal, Convert.ToInt32(floatVal));Console.WriteLine("Double value: {0}, Int32 value: {1}", doubleVal, Convert.ToInt32(doubleVal));}}
Let's look at the explanation of the code above:
Convert.ToInt32()
method to convert each of the floating-point values to 32-bit integer values and print the results to the console.