Dart Basic- Numbers

Numbers in Dart

Numbers can be classified in Dart 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.

Example:

void main() {
   // declare an integer
   int num1 = 10;            
     
   // declare a double value
   double num2 = 10.50;  
 
   // print the values
   print(num1);
   print(num2);
}

It will produce the following output −

10
10.5

Parsing

The parse() static function allows parsing a string containing a numeric literal into a number. The following illustration demonstrates the same −

void main() {
   print(num.parse('12'));
   print(num.parse('10.91'));
}

The above code will result in the following output −

12
10.91

The parse function throws a FormatException if it is passed any value other than numerals. The following code shows how to pass an alpha-numeric value to the parse() function.

NoProperty & Description
1hashcode
Returns a hash code for a numerical value.
2isFinite
True if the number is finite; otherwise, false.
3isInfinite
True if the number is positive infinity or negative infinity; otherwise, false.
4isNan
True if the number is the double Not-a-Number value; otherwise, false.
5isNegative
True if the number is negative; otherwise, false.
6signReturns
minus one, zero or plus one depending on the sign and numerical value of the number.
7isEvenReturns
true if the number is an even number.
8isOddReturns
true if the number is an odd number.

Example:

void main() {
  double num1 = -3.14;
  double num2 = 2.71828;
  int num3 = 7;
    // hashCode
  print("Hash code of num2: ${num2.hashCode}");
  
  // isFinite
  print("Is num1 finite? ${num1.isFinite}");
  
  // isInfinite
  print("Is num2 infinite? ${num2.isInfinite}");
  
  // isNaN
  double nan = 0.0/0.0;
  print("Is $nan Not-a-Number? ${nan.isNaN}");
  
  // isNegative
  print("Is num1 negative? ${num1.isNegative}");
  
  // sign
  print("Sign of num1: ${num1.sign}");
  
  // isEven
  print("Is num3 even? ${num3.isEven}");
  
  // isOdd
  print("Is num3 odd? ${num3.isOdd}");
}

Output:

Hash code of num2: 529171446
Is num1 finite? true
Is num2 infinite? false
Is NaN Not-a-Number? true
Is num1 negative? true
Sign of num1: -1
Is num3 even? false
Is num3 odd? true

Number Methods

Given below is a list of commonly used methods supported by numbers −

NoMethod & Description
1abs
Returns the absolute value of the number.
2ceil
Returns the least integer no smaller than the number.
3compareTo
Compare this to other numbers.
4Floor
Returns the greatest integer not greater than the current number.
5remainder
Returns the truncated remainder after dividing the two numbers.
6Round
Returns the integer closest to the current numbers.
7toDouble
Returns the double equivalent of the number.
8toInt
Returns the integer equivalent of the number.
9toString
Returns the string equivalent representation of the number.
10truncate
Returns an integer after discarding any fractional digits.

Example:

void main() {
  double num1 = -3.14;
  double num2 = 2.71828;
  int num3 = 7;
  
  // absolute value
  print("Absolute value of num1: ${num1.abs()}");
  
  // ceil
  print("Ceil value of num2: ${num2.ceil()}");
  
  // compareTo
  print("Compare num2 with num3: ${num2.compareTo(num3)}");
  
  // floor
  print("Floor value of num2: ${num2.floor()}");
  
  // remainder
  print("Remainder of num3 divided by num2: ${num3.remainder(num2)}");
  
  // round
  print("Rounded value of num2: ${num2.round()}");
  
  // toDouble
  print("Double value of num3: ${num3.toDouble()}");
  
  // toInt
  print("Integer value of num2: ${num2.toInt()}");
  
  // toString
  print("String representation of num1: ${num1.toString()}");
  
  // truncate
  print("Truncated value of num2: ${num2.truncate()}");
  
}

Output:

Absolute value of num1: 3.14
Ceil value of num2: 3
Compare num2 with num3: -1
Floor value of num2: 2
Remainder of num3 divided by num2: 1.56344
Rounded value of num2: 3
Double value of num3: 7
Integer value of num2: 2
String representation of num1: -3.14
Truncated value of num2: 2
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.