Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values:

  1. int
  2. double

1. int data type

int data type supports integer values, and their size or values range is platform-dependent. Integers are numbers without a decimal point. On the native platforms, values range from -263  to 263 – 1. On the web platforms, integer values correspond to JavaScript numbers that range from -253 to 253 – 1.

Example,

int time = 2; 
int x = 5e2;

2. double data type

double data type supports 64-bit floating-point numbers, basically decimal values.

Example,

double rate = 8.5;
double pi = 3.14;
double x = 1;

In the last example, the compiler performs implicit type-casting, converting integer value into decimal whenever required.

Instead of using keywords ‘int’ and ‘double’ separately for integer and decimal values, respectively, we can simply use the ‘num’ keyword.

Variable declared with num keyword can have both integer and decimal values.

num area = 9;
num area = 9.9;

The num data type provides basic operators like +, -, /, and * and the int data type specifies bitwise operators like bitwise left shift and right shift (<< , >>), AND (&), OR (|), and XOR (^).

Example:

Consider the following code in Dart to understand the above points more clearly,

import ‘dart:io’;
void main( )
{
  // no type annotation. Compiler automatically infers integer value
  var a = 5 ;


  // Compiler automatically infers double value
  var b = 5.5 ;
  
  // declaring c as integer type
  int c = 6 ;
  
  // declaring variables as double type
  double d = 6.2 ;
  double e = 5 ; // compiler implicitly converts integer into double value when required
  
  /* with num compiler automatically interprets integer or double type by
  analyzing the value assigned */
  num f = 9 ;
  
  num g = 9.9 ;
  
  print( ' \n Value of a : $a \n Value of b : $b \n Value of c : $c \n Value of d : $d \n Value of e : $e \n Value of f : $f \n Value of g : $g ' );
}

Output :

Value of a : 5   
Value of b : 5.5 
Value of c : 6   
Value of d : 6.2 
Value of e : 5.0 
Value of f : 9   
Value of g : 9.9

Constructors :

  • num(  )

Properties of num class :

PropertiesDescription
  hashcodeReturns the hash code for a numerical value. It returns the same value for int and double when the value provided is the same. Implementation = int get hashchode;  
  isFiniteChecks the finiteness of the number and accordingly returns true or false. All integers are finite. The non-finite numbers are NaN values, positive infinity, and negative infinity only.
Implementation = bool get isFinite; 
  isInfiniteChecks the infiniteness : positive or negative, of the number and returns true or false accordingly.
Implementation = bool get isInfinite;
    isNanChecks whether the number is NaN ( Not-a-Number value ) and returns true or false accordingly.
Implementation = bool get isNan; 
    isNegativeChecks whether the number is negative or not and returns true or false accordingly.
Implementation = bool get isNegative;
      signAssesses the sign of the number and returns -1, 0, or 1 accordingly.
-1 = number is less than zero; negative 
1 = number is greater than zero; positive 
0 = number is equal to zero
Implementation = num get sign;
    isevenChecks whether a number is even and returns true or false accordingly.
Implementation = bool get isEven; 
    isOddChecks whether a number is odd and returns true or false accordingly.
Implementation = bool get isOdd; 

Methods

MethodsDescription
    abs( )Returns the absolute value of the given number, which is the number itself.
Implementation = num abs( ); 
    ceil( )Returns the least integer not smaller than the given number. It basically rounds the number to the positive infinity. Implementation = int ceil( );  
  clamp( num lowerLimit, num upperLimit )Returns the given number clamped in the range lowerLimit – upperLimit.
Implementation = clamp( num lowerLimit, num upperLimit ); 
    compareTo( num other )Compares the given number with other and returns a negative number if the given number is less than the other, a positive value if the given number is greater than the other, and zero if they are equal.
Implementation = int compareTo( num other );
    floor( )Returns the greatest integer not greater than the given number. It basically rounds the number to the negative infinity. Implementation = int floor( );  
  remainder( num other )Returns the remainder of the division of a given number by other.
Implementation = num remainder( num other );
    round( )Returns the integer value closest to the given number. The number must be finite in order to use this method. Implementation = int round( );  
    toDouble( )Converts a given number into double and returns the value of double type. In case an integer value can not be represented as double accurately, an approximation is returned.
Implementation = double toDouble( ); 
  toInt( )Converts a given number into int and returns the value of int type.
Implementation = int toInt( ); 
  toString( )Converts a given number into an equivalent shortest string and returns the same.
Implmentation = String toString( );
  truncate( )Returns the integer after discarding or truncating the fractional part of the given number. It rounds fractional values to zero.
Implementation = int truncate( );