Open In App

Basics of Numbers in Dart

Last Updated : 06 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Like other languages, Dart Programming also supports numerical values as Number objects. The number in Dart Programming is the data type that is used to hold the numeric value. Dart numbers can be classified as: 

n3umdrawio

int: The int data type is used to represent whole numbers.

Syntax: int var_name;

double: The double data type is used to represent 64-bit floating-point numbers.

Syntax: double var_name;

Example 1: 
 

Dart




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


Output: 

2
1.5

Note: The num type is an inherited data type of the int and double types.

Parsing in Dart: The parse() function is used parsing a string containing numeric literal and convert to the number. 
 

Example 2: 
 

Dart




void main()
{
   
  var a1 = num.parse("1"); 
  var b1 = num.parse("2.34"); 
 
  var c1 = a1+b1;  
  print("Product = ${c1}"); 


Output: 

Product = 3.34

Properties: 

  • hashcode: This property is used to get the hash code of the given number.
  • isFinite: If the given number is finite, then this property will return true.
  • isInfinite: If the number is infinite, then this property will return true.
  • isNan: If the number is non-negative then this property will return true.
  • isNegative: If the number is negative then this property will return true.
  • sign: This property is used to get -1, 0, or 1 depending upon the sign of the given number.
  • isEven: If the given number is an even then this property will return true.
  • isOdd: If the given number is odd then this property will return true.

Methods: 

  • abs(): This method gives the absolute value of the given number.
  • ceil(): This method gives the ceiling value of the given number.
  • floor(): This method gives the floor value of the given number.
  • compareTo(): This method compares the value with other numbers.
  • remainder(): This method gives the truncated remainder after dividing the two numbers.
  • round(): This method returns the round of the number.
  • toDouble(): This method gives the double equivalent representation of the number.
  • toInt(): This method returns the integer equivalent representation of the number.
  • toString(): This method returns the String equivalent representation of the number
  • truncate(): This method returns the integer after discarding fraction digits.

Some try-error for num:

1)

Dart




void main(){
 
    int x = 10;
    x = 20.5; // Error: A value of type 'double' can't be assigned to
      // a variable of type 'int'.
 
    num y = 10;
    y = 10.5; // No Error
}


That means we can use num data type if we don't know the actual type of input, if it might be an int or double.

2)

Dart




void main(){
 
    double y = 10; // We can store the int value into the double data type but
      // we can't store double value in int type.
    y = 10.5;
}


We can store the int value into the double data type but we can't store double value in int type.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads