×

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( );

Related Topics

Dart Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 minutes read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Dart Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 minutes read.

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Dart Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

2 minutes read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.

Dart Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Dart Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Dart Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.