Dart Number Methods
Dart Number Methods
In Dart, multiple number methods are built-in and useful when dealing with numbers. This tutorial will examine some of the most frequently used methods.
Dart Number Methods
The commonly used number methods in Dart programming language are as follows:
Number Method | Method Description |
abs() | The abs() method returns the absolute value of the given number. |
ceil() | The ceil() method returns the ceiling value of the given number. It returns the smallest integer greater than or equal to the given number. |
floor() | The floor() method returns the floor value of the given number. It returns the largest integer less than or equal to the given number. |
round() | The round() method returns the round of the number. This method returns the nearest integer. The round() method rounds to the nearest even number in case of a tie. For example, 5.5 would be rounded to 6. |
compareTo() | This compareTo() method compares the value with another number |
remainder() | The remainder() method returns the truncated remainder after dividing the two numbers. |
toDouble() | The toDouble() method returns the double equivalent representation of the given number. |
toInt() | The toInt() method returns the integer equivalent representation of the given number. This method converts a double to an integer by discarding the fractional part of the double number. |
toString() | The toString() method returns the String equivalent representation of the given number. |
truncate() | The truncate() method returns the integer after discarding the fraction digits. |
parse() | The parse() method converts and returns the numeric value of the String. |
Example
abs()
The abs() method returns the absolute value of the given number.
Sample Dart program to show the usage of the abs() method.
/*
Dart Number Methods demo Program
Dart Tutorials – www.TestingDocs.com
*/
void main()
{
// declare a negative number
int negative_number = -7;
int abs_number = negative_number.abs();
print(abs_number);
}
We can use these methods to perform various mathematical operations and
conversions in the Dart programs.
Dart Tutorials
Dart tutorial on this website can be found at:
https://www.testingdocs.com/dart-tutorials-for-beginners/