In this tutorial you will learn about the Dart Numbers and its application with practical example.
Dart Numbers
The Number data type is used to hold the numeric values. Dart supports following numerical data types –
- Dart Integer
- Dart Double
Dart Integer:- Dart Integers are used to store whole numbers. Dart integer data type is used to represent 64 bit non-decimal number between -263 to 263 – 1. An integer can be used to store either signed and unsigned integer value. Integers can be declared using int keyword.
1 |
int age = 25; |
Dart Double :- Dart double is used to represent a 64-bit (double-precision) floating-point numbers or numbers with larger decimal points. Double can be declared using double keyword.
1 |
double pi = 3.14; |
Example:-
1 2 3 4 5 6 7 |
void main() { int radius = 2; // declare an integer double pi = 3.14; double area = pi*radius*radius; print("Area Of Circle Is :${area}"); } |
Output:-
1 |
Area Of Circle Is :12.56 |
Rules for integers:-
- An integer must have at least one digit
- Integers are numbers without a decimal point
- An integer can be either positive or negative
- Integer values no larger than 64 bits, depending on the platform.
Dart parse() function
The Dart parse() function converts a numeric string into a number.
Example:-
1 2 3 4 |
void main() { print(num.parse("15")); print(num.parse("15.75")); } |
Output:-
1 2 |
15 15.75 |
The parse() function throws a FormatException if it is passed any value other than numerals.
Number Properties
Below is a list of properties supported by Dart numbers.
Property | Description |
---|---|
hashcode |
It Returns a hash code for a numerical value provided. |
isFinite |
It returns True if the number is finite; otherwise, false. |
isInfinite |
It returns True if the number is positive infinity or negative infinity; otherwise, false. |
isNan |
It returns True if the number is a Not-a-Number value; otherwise, false. |
isNegative |
It returns True if the number is negative; otherwise, false. |
sign |
It returns minus one, zero or plus one depending on the sign and numerical value of the number. |
isEven |
It returns true if the number is an even number. |
isOdd |
It returns true if the number is an odd number. |
Number Methods
Below is a list of commonly used methods supported by Dart numbers.
Method | Description |
---|---|
abs |
It returns the absolute value of the given number. |
ceil |
It returns the least integer no smaller than the number. |
Floor |
It returns the greatest integer not greater than the given number. |
compareTo |
It is used to compare the with other number. |
remainder |
It returns the truncated remainder after dividing the two numbers. |
Round |
It returns the integer closest to the current numbers. |
toDouble |
It returns the double equivalent of the number. |
toInt |
It returns the integer equivalent of the number. |
toString |
It returns the string equivalent representation of the number. |
truncate |
It returns an integer after discarding any fractional digits. |