Number

The Number is the data type that is used to hold the numeric value. In Dart, It can be two types –

  • Integer
  • Double
Dart Number

Dart integer – Integer numbers are the whole numbers means that can be written without a fractional component. For example – 20, 30, -3215, 0, etc. An integer number can be signed or unsigned. The representation of the integer value is in between -263 to 263 non-decimal numbers. The int keyword is used to declare integer value in Dart.

int id = 501;   

Dart Double – The Double number are the numbers that can be written with floating-point numbers or number with larger decimal points. The double keyword is used to declare Double value in Dart.

double root = 1.41234;  

or  

double rupees  = 100000;

Rules for the integer value

  • An integer value must be a digit.
  • The decimal points should not include in an integer number.
  • Unsigned numbers are always a positive number. Numbers can be either negative or positive.
  • The size of the integer value depends upon the platform, but integer value should no longer than 64 bit.

Let’s have a look at following example –

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 parse() function converts the numeric string to the number. Consider the following 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 above example, we converted the numeric strings into the numbers by using parse() method then stored in the variables. After the successful conversion, we performed add operation and printed the output to 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 commonly used methods of number are given below.

    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.

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *