Add Your Heading Text Here

Dart Number is the Super type(parent) of the integer and double.

Starting with the first data type numbers, which is divided in to 2 types,

  1. Integer
  2. Double

Numbers can have value from -2^63 to 2^3-1 but as we all know that Dart is converted to javascript directly so, it allows only values in range from -2^53 to 2^53-1. Beyond this range Dart VM will give you a garbage value.

For ex :

Numbers can be 1,-5,3.5,-45.25 etc. So, In this tutorial we will briefly discuss about integer as well as doubles.

Integer

Integer contains all the numbers which does not have decimal values.

You cannot assign the decimal value(fractional value) to an integer data type variable. Dart VM will give you a compile time error if you do so.

Integer variable cannot have values larger than 64 bits. Dart has integer data type range from (-2^63) to (2^63-1) (i.e,-9223372036854775808 to +9223372036854775807) which means you cannot assign the value beyond this range(-2^63 to 2^63-1) to the variable. By, assigning value beyond this range Dart VM will you give you compile time error.

To declare any varible as integrer, int keyword is used.

For ex :

int variableName=7;

User can also provide hex values to the integer variables. which is used to store different color codes.

For ex :

int hex = 0xABCDEF;

User can also perform bitwise shift(<<,>>) ,AND(&) and OR(|) operators. we will briefly discuss about it later.

For ex :

print(3<<6); // which gives value 192

Double

64-bit (double-precision) floating-point numbers, as specified by the IEEE 754 standard.

Integer cannot have fractional(decimal) values. so, double is introduced to achieve the desired requirements.

To declare any variable as Double, double keyword is used.

For ex :

double variableName = 7.7;

User can also store exponential values to the integer variables. which is used to store different color codes.

For ex :

double value= 1.42e5; // value=142000
where, e is the 10 base to power 5

User can perform basic arithmatic operations(+,-,*,/) with the Number data types(int and double included).

The operations performed using either int or double data type can also be performed using num data type as Number is the SuperClass for int and double.

Parsing Numbers

The parse() static function is used to get the number values(either int or double) stored in String variable.

For ex :

void main() {
  String i='10';
  print(num.parse(i)); 
  print(num.parse('10.10')); 
}

User cannot parse any string variables with values other than numeric values.

Number Common Properties

For ex :

void main() {
  int x1=556;
  int x2=-200;
  double y1=121.6;
  double y2=-1000.45;
  
  print(x1.isEven);
  print(x1.isOdd);
  
  print(x1.hashCode);
  print(y1.hashCode);
  
  print(x1.bitLength);
  
  print(x1.isFinite);
  print(y1.isFinite);
  
  print(x1.isInfinite);
  print(y1.isInfinite);
  
  print(x1.isNaN);
  print(y1.isNaN);
  
  print(x1.isNegative);
  print(y1.isNegative);
  
  print(x1.runtimeType);
  print(y1.runtimeType);
  
  print(x2.sign);
  print(y2.sign);
  
}

Output :

[true, false, 556, 121, 10, true, true, false, false, false, false, false, false, int, double, -1, -1]

Number Common Methods

For ex :

void main() {
  int x1=556;
  int x2=200;
  int x3=-9999;
  double y1=121.6;
  double y2=1000.45;
  double y3=-6666.66;
  
  print(x1.toString());
  print(y1.toString());
  
  print(x1.toRadixString(5));
  
  print(x3.abs());
  print(y3.abs());
  
  print(x3.toUnsigned(3));
  print(x3.toSigned(2));
  
  print(x1.round());
  print(y1.round());
  
  print(x1.floor());
  print(y1.floor());
  
  print(x1.ceil());
  print(x2.ceilToDouble());
  print(y1.ceil());
  print(y2.ceilToDouble());

  print(x1.compareTo(x2));
  print(y1.compareTo(y2));
  
  print(x1.gcd(x2));
  
  print(x1.remainder(x2));
  print(y1.remainder(y2));
  
  print(x1.toDouble());
  print(y1.toInt());
  
}

Output :

[4211, 556, 121.6, 9999, 6666.66, 1, 1, 556, 122, 556, 121, 556, 200, 122, 1001, 1, -1, 4, 156, 121.6, 556, 121]

So, we can apply Numbers in many ways ,either as int or double to achieve our desired requirements. Further, we will discuss about Strings in Dart.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons