How to use the toInt() function in Dart
Overview
The toInt() function converts the number to an integer and returns it.
The following image shows how toInt() works in Dart.
Syntax
number.toInt()
Parameter
This function does not require a parameter.
Return value
toInt() returns the integer part of a number.
Example
The code below shows the use of the toInt() function in Dart.
import 'dart:convert';void main() {//positive numberprint("The value of (1.5).toInt(): ${(1.5).toInt()} ");// negative numberprint("The value of (-5.6).toInt(): ${(-5.6).toInt()} ");//zeroprint("The value of (0).toInt(): ${(0).toInt()}");}
Explanation
- Line 1: We import the
data:convertlibrary. - Line 3: We create the
void mainfunction.
- Line 5: We calculate the integer value of the positive number
1.5usingtoInt().
- Line 8: We calculate the integer value of the negative number
-5.6usingtoInt().
- Line 11: We calculate the integer value
0usingtoInt().