Search

# Dart Basic- Numbers

Home /

## Numbers in Dart

Numbers can be classified in Dart as-

• int − Integer of arbitrary size. The int data type is used to represent whole numbers.
• double − 64-bit (double-precision) floating-point numbers, as specified by the IEEE 754 standard. The double data type is used to represent fractional numbers

The num type is inherited by the int and double types. The dart core library allows numerous operations on numeric values.

Example:

``````void main() {
// declare an integer
int num1 = 10;

// declare a double value
double num2 = 10.50;

// print the values
print(num1);
print(num2);
}
``````

It will produce the following output −

```10
10.5

```

### Parsing

The parse() static function allows parsing a string containing a numeric literal into a number. The following illustration demonstrates the same −

``````void main() {
print(num.parse('12'));
print(num.parse('10.91'));
}
``````

The above code will result in the following output −

```12
10.91

```

The parse function throws a FormatException if it is passed any value other than numerals. The following code shows how to pass an alpha-numeric value to the parse() function.

Example:

``````void main() {
double num1 = -3.14;
double num2 = 2.71828;
int num3 = 7;
// hashCode
print("Hash code of num2: \${num2.hashCode}");

// isFinite
print("Is num1 finite? \${num1.isFinite}");

// isInfinite
print("Is num2 infinite? \${num2.isInfinite}");

// isNaN
double nan = 0.0/0.0;
print("Is \$nan Not-a-Number? \${nan.isNaN}");

// isNegative
print("Is num1 negative? \${num1.isNegative}");

// sign
print("Sign of num1: \${num1.sign}");

// isEven
print("Is num3 even? \${num3.isEven}");

// isOdd
print("Is num3 odd? \${num3.isOdd}");
}
``````

Output:

``````Hash code of num2: 529171446
Is num1 finite? true
Is num2 infinite? false
Is NaN Not-a-Number? true
Is num1 negative? true
Sign of num1: -1
Is num3 even? false
Is num3 odd? true``````

### Number Methods

Given below is a list of commonly used methods supported by numbers −

Example:

``````void main() {
double num1 = -3.14;
double num2 = 2.71828;
int num3 = 7;

// absolute value
print("Absolute value of num1: \${num1.abs()}");

// ceil
print("Ceil value of num2: \${num2.ceil()}");

// compareTo
print("Compare num2 with num3: \${num2.compareTo(num3)}");

// floor
print("Floor value of num2: \${num2.floor()}");

// remainder
print("Remainder of num3 divided by num2: \${num3.remainder(num2)}");

// round
print("Rounded value of num2: \${num2.round()}");

// toDouble
print("Double value of num3: \${num3.toDouble()}");

// toInt
print("Integer value of num2: \${num2.toInt()}");

// toString
print("String representation of num1: \${num1.toString()}");

// truncate
print("Truncated value of num2: \${num2.truncate()}");

}``````

Output:

``````Absolute value of num1: 3.14
Ceil value of num2: 3
Compare num2 with num3: -1
Floor value of num2: 2
Remainder of num3 divided by num2: 1.56344
Rounded value of num2: 3
Double value of num3: 7
Integer value of num2: 2
String representation of num1: -3.14
Truncated value of num2: 2``````

#### Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.