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.

A pictorial representation of toInt() function

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 number
print("The value of (1.5).toInt(): ${(1.5).toInt()} ");
// negative number
print("The value of (-5.6).toInt(): ${(-5.6).toInt()} ");
//zero
print("The value of (0).toInt(): ${(0).toInt()}");
}

Explanation

  • Line 1: We import the data:convert library.
  • Line 3: We create the void main function.
  • Line 5: We calculate the integer value of the positive number 1.5 using toInt().
  • Line 8: We calculate the integer value of the negative number -5.6 using toInt().
  • Line 11: We calculate the integer value 0 using toInt().