thumbnail

Numbers in Dart

Published on 29 March 2020
Last Updated on 29 March 2020

Types of numbers in Dart

Dart supports following two different types of numbers:

  • int
  • double

1. Int

Maximum storage size for integers is 64 bits, depending on the platform. Int data type is used for storing integers of arbitrary size representing whole numbers.

int is a sub-type of type num.

2. Double

Double is a double precision floating point number, as specified by the IEEE 754 standard.

Double is a sub-type of type num.

Because, both int and double are sub-types of type num, methods available for num data type such as abs(), ceil() and `floor()’, among other methods are available for both data types.

Program that demonstrates how to use numbers in Dart

void main() {
  // integer 1
  var numberOne = 1;
  print('numberOne is');
  print(numberOne);
  print('\n');

  // integer 100
  var hex = 0x64;
  print('hex is');
  print(hex);
  print('\n');

  // please note that, since dart 2.1, integer literals are automatically converted to double
  // Equivalent to doubleParsedFromInteger = 1.0.
  double doubleParsedFromInteger = 1;
  print('doubleParsedFromInteger is');
  print(doubleParsedFromInteger);
  print('\n');

  // please note that int and double are sub types of the type num
  num xyz1 = 2;
  num xyz2 = 2.0;
  print('xyz1 is');
  print(xyz1);
  print('xyz2 is');
  print(xyz2);
  print('\n');

  // demonstration of how a string can be parsed as int
  var zeroAsInt = int.parse('0'); // Equivalent to zeroAsInt = 0
  print('zeroAsInt is');
  print(zeroAsInt);
  print('\n');

  // string to double conversion
  var onePointOne = double.parse('1.1'); // Equivalent to onePointOne = 1.1
  print('onePointOne is');
  print(onePointOne);
  print('\n');

  // use num.prase if you are unsure whether the numbers is int or double
  var anyNum = num.parse('1.22');
  print('anyNum is');
  print(anyNum);
  print('\n');

  var y = 1.1;
  print('y is');
  print(y);
  print('\n');
  // 1.0
  var exponentExample = 0.01e2;
  print('exponentExample is');
  print(exponentExample);
  print('\n');

  // numbers that are compile time constants
  const numOne = 1;
  const numTwo = 2;
  const onePlusTwo = numOne + numTwo;
  print('numOne is');
  print(numOne);
  print('numTwo is');
  print(numTwo);
  print('onePlusTwo is');
  print(onePlusTwo);
  print('\n');
}

Above program produces following output:

numberOne is
1

hex is
100

doubleParsedFromInteger is
1.0

xyz1 is
2
xyz2 is
2.0

zeroAsInt is
0

onePointOne is
1.1

anyNum is
1.22

y is
1.1

exponentExample is
1.0

numOne is
1
numTwo is
2
onePlusTwo is
3

Literals are compile time constants

Above program makes use of following code:

const numOne = 1;
const numTwo = 2;
const onePlusTwo = numOne + numTwo;

Above literal numbers are compile-time constants in Dart.

Number parsing errors

Code:

var zeroAsInt = int.parse('0ABC'); // parsing error
var onePointOne = double.parse('ABC1.1'); // parsing error
var anyNum = num.parse('1.22ABC'); // parsing error

Above program produces following output:

Unhandled exception:
FormatException: Invalid radix-10 number (at character 1)
0ABC

Explanation: Above program demonstrates the issue while trying to parse string as numbers in Dart that are not formatted as number.

Take this issue into consideration while writing Dart code that interact with external input that might differ from accepted number formats in Dart.