Dart Programming - Numbers

Dart numbers can be classified as −

  • int − Integer of arbitrary size. The int data type is used to represent whole numbers.

  • double − 64-bit (double-precision) floating-point numbers, as specified by the IEEE 754 standard. The double data type is used to represent fractional numbers

The num type is inherited by the int and double types. The dart core library allows numerous operations on numeric values.

Syntax 

int var_name;      // declares an integer variable 
double var_name;   // declares a double variable 

 

Example

void main() {
   // declare an integer
   int a= 10;             
     
   // declare a double value
   double b= 20.21;  

   // print the values
   print(a);
   print(b);
}

 

Output

10 
20.21 

 

Parsing

The parse() static function allows parsing a string containing numeric literal into a number

 

void main() { 
   print(num.parse('11')); 
   print(num.parse('11.91')); 
}

 

Output

11 
11.91

 

Number Properties

Properties supported by Dart numbers.

Sr.No Property & Description
1 hashCode

Returns a hash code for a numerical value.

2 isFinite

True if the number is finite; otherwise, false.

3 isInFinite

True if the number is positive infinity or negative infinity; otherwise, false.

4 isNan

True if the number is the double Not-a-Number value; otherwise, false.

5 isNegative

True if the number is negative; otherwise, false.

6 sign

Returns minus one, zero or plus one depending on the sign and numerical value of the number.

7 isEven

Returns true if the number is an even number.

8 isOdd

Returns true if the number is an odd number.

 

Number Methods

Number mtethods are below

Sr.No Method & Description
1 abs

Returns the absolute value of the number.

2 ceil

Returns the least integer no smaller than the number.

3 compareTo

Compares this to other number.

4 floor

Returns the greatest integer not greater than the current number.

5 remainder

Returns the truncated remainder after dividing the two numbers.

6 round

Returns the integer closest to the current numbers.

7 toDouble

Returns the double equivalent of the number.

8 toInt

Returns the integer equivalent of the number.

9 toString

Returns the string equivalent representation of the number.

10 truncate

Returns an integer after discarding any fractional digits.