Last updated on February 21, 2023
The toInt() method converts non-integer values into integers. It doesn’t accept any parameters and returns value with integer data type.
Syntax
number.toInt()
Parameters
The toInt() method doesn’t need any parameter, it attaches with a value or variable to convert into the integer.
Example 1
Let’s say we have a float value,
65.10
To convert into integer, attach the toInt() method.
65.10.toInt();
// Output: 65
You can also wrap the value with parenthesis,
(65.10).toInt();
// Output: 65
In this example, the toInt() method is attached with a float value which is converted into an integer. Therefore we got 65 in the output. You can use the print() function to see the result in the console.
print((65.10).toInt());
Example 2
Create a variable and store a float value.
var floatValue = 30.15;
To convert into integer, attach the toInt() method with floatValue variable which can be accessed through dot notation.
floatValue.toInt();
// Output: 30
To see the result in the console, use print() function.
print(floatValue.toInt());