Dart Data Types

Dart Number

The data type used to store the numerical value is called Number. It might be one of two kinds in dart:

  • Integer
  • Double
Dart Number -

Dart integer: Whole numbers that may be expressed without a fractional part are known as integers. Take -20, 30, -3215, 0, etc. as examples. A signed or unsigned integer can exist. The integer value is represented by a range of non-decimal values, from -263 to 263. In Dart, an integer value is declared using the int keyword.

				
					int id = 501;   
				
			

Dart Double: The Double numbers are those that have larger decimal points or may be represented in floating-point notation. In Dart, a Double value is declared with the double keyword.

				
					double root = 1.41234;  
or  
double rupees  = 100000;  
				
			

Rules for the integer value

  • The value of an integer must be a digit.
  • An integer number shouldn’t contain decimal points.
  • Positive numbers are invariably unsigned. Positive or negative numbers are both possible.
  • Although the size of the integer value varies depending on the platform, it should not be more than 64 bits.

Let’s examine the following illustration:

Example

				
					void main(){    
 int r = 5;  
 double pi = 3.14;  
 double res = 4*pi*r*r;    
 print("The area of sphere = ${(res)}");  
}  
				
			

Output

				
					The area of sphere 314 
				
			

Dart parse() function

The numeric string is changed to a number using the parse() method. Take a look at this example:

Example

				
					void main(){  
var a = num.parse("20.56");  
var b = num.parse("15.63");  
  
var c = a+b;   
print("The sum is = ${c}");  
}  
				
			

Output

				
					The sum is = 36.19
				
			

Explanation

In the example above, we used the parse() method to transform the numeric strings into numbers, which were then put in the variables. Following the conversion’s successful completion, we added the result and displayed it on the screen.

Number Properties

PropertiesDescription
hashcodeIt returns the hash code of the given number.
isFiniteIf the given number is finite, then it returns true.
isInfiniteIf the number infinite it returns true.
isNanIf the number is non-negative then it returns true.
isNegativeIf the number is negative then it returns true.
signIt returns -1, 0, or 1 depending upon the sign of the given number.
isEvenIf the given number is an even then it returns true.
isOddIf the given number is odd then it returns true.

Number Methods

The following lists the frequently used numbering techniques.

MethodDescription
abs()It gives the absolute value of the given number.
ceil()It gives the ceiling value of the given number.
floor()It gives the floor value of the given number.
compareTo()It compares the value with other number.
remainder()It gives the truncated remainder after dividing the two numbers.
round()It returns the round of the number.
toDouble()It gives the double equivalent representation of the number.
toInt()Returns the integer equivalent representation of the number.
toString()Returns the String equivalent representation of the number
truncate()Returns the integer after discarding fraction digits.
Share this Doc

Dart Number

Or copy link

Explore Topic