×

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 Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 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 Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 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 Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 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.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

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.

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.

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 Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes 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.

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.

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.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Dart Packages

Every programming language has some in-built functions stored inside the header files that makes the task much easier for the programmer. The Dart Packages refer to the compilation of a...

4 minutes read.

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes 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.