Basics of Numbers in Dart
Like other languages, Dart Programming also supports numerical values as Number objects. The number in Dart Programming is the data type that is used to hold the numeric value.
Dart numbers can be classified as:
int (Integer)
The int data type is used to represent whole numbers.
Declaring Integer in Dart
int var_name;
Example :
// Declaring an integer variable
int age = 25;
double (Floating-Point Number)
The double data type is used to represent 64-bit floating-point numbers.
Declaring Double in Dart
double var_name;
Example :
// Declaring a double variable
double pi = 3.1415;
Example Usage :
void main() {
// declare an integer
int num1 = 2;
// declare a double value
double num2 = 1.5;
// print the values
print(num1);
print(num2);
}
Output:
2
1.5
Note: The
num
is actually the superclass ofint
anddouble
, meansint
anddouble
extendnum
.
Parsing in Dart
The parse()
function converts a valid numeric string into a number, but if the string is not numeric, it throws a FormatException
.
Example :
void main() {
// Parsing string values into
// numeric types using num.parse()
// Converts "1" into a numeric value (int)
var a1 = num.parse("1");
// Converts "2.34" into a numeric value (double)
var b1 = num.parse("2.34");
// Performing addition operation on parsed numbers
var c1 = a1 + b1;
// Printing the result of the addition
print("Sum = ${c1}");
// Output: Sum = 3.34
}
Output:
Sum = 3.34
Properties of Numbers in Dart
Property | Description |
---|---|
hashcode | This property is used to get the hash code of the given number |
isFinite | If the given number is finite, then this property will return true |
isInfinite | If the number is infinite, then this property will return true |
isNan | (Not a Number) returns |
isNegative | If the number is negative then this property will return true |
sign | This property is used to get -1, 0, or 1 depending upon the sign of the given number |
isEven | If the given number is an even then this property will return true |
isOdd | If the given number is odd then this property will return true |
Implementation of Properties:
void main() {
int num1 = 10;
double num2 = -5.5;
double num3 = double.infinity;
// NaN (Not a Number)
double num4 = 0 / 0;
// hashCode: Returns the hash
// code of the number
print("Hash code of num1: \${num1.hashCode}");
print("Hash code of num2: \${num2.hashCode}");
// isFinite: Returns true if
// the number is finite
print("Is num1 finite? \${num1.isFinite}"); // true
print("Is num3 finite? \${num3.isFinite}"); // false (infinity is not finite)
// isInfinite: Returns true if
// the number is infinite
print("Is num3 infinite? \${num3.isInfinite}"); // true
print("Is num1 infinite? \${num1.isInfinite}"); // false
// isNaN: Returns true if
// the number is Not-a-Number (NaN)
print("Is num4 NaN? \${num4.isNaN}"); // true
print("Is num1 NaN? \${num1.isNaN}"); // false
// isNegative: Returns true if
// the number is negative
print("Is num2 negative? \${num2.isNegative}"); // true
print("Is num1 negative? \${num1.isNegative}"); // false
// sign: Returns -1 for negative,
// 0 for zero, and 1 for positive numbers
print("Sign of num1: \${num1.sign}"); // 1
print("Sign of num2: \${num2.sign}"); // -1
print("Sign of 0: \${0.sign}"); // 0
// isEven: Returns true if
// the number is even
print("Is num1 even? \${num1.isEven}"); // true (10 is even)
print("Is num2 even? \${num2.toInt().isEven}"); // false (-5 is odd)
// isOdd: Returns true if
// the number is odd
print("Is num1 odd? \${num1.isOdd}"); // false (10 is even)
print("Is num2 odd? \${num2.toInt().isOdd}"); // true (-5 is odd)
}
Output:
Hash code of num1: 10
Hash code of num2: 250189098
Is num1 finite? true
Is num3 finite? false
Is num3 infinite? true
Is num1 infinite? false
Is num4 NaN? true
Is num1 NaN? false
Is num2 negative? true
Is num1 negative? false
Sign of num1: 1
Sign of num2: -1
Sign of 0: 0
Is num1 even? true
Is num2 even? false
Is num1 odd? false
Is num2 odd? true
Methods of Numbers in Dart
Method | Description |
---|---|
abs() | This method gives the absolute value of the given number |
ceil() | This method gives the ceiling value of the given number |
floor() | This method gives the floor value of the given number |
compareTo() | This method compares the value with other numbers |
remainder() | This method gives the truncated remainder after dividing the two numbers |
round() | This method returns the round of the number |
toDouble() | This method gives the double equivalent representation of the number |
toInt() | This method returns the integer equivalent representation of the number |
toString() | This method returns the String equivalent representation of the number |
truncate() | This method returns the integer after discarding fraction digits |
Implementation of methods :
void main() {
double number = -12.75;
int intNumber = 15;
// abs() - Returns absolute value
print('Absolute Value: ${number.abs()}'); // Output: 12.75
// ceil() - Returns the smallest integer
// greater than or equal to the number
print('Ceiling Value: ${number.ceil()}'); // Output: -12
// floor() - Returns the largest integer
// less than or equal to the number
print('Floor Value: ${number.floor()}'); // Output: -13
// compareTo() - Compares the number
// with another number
print('Compare To (10): ${number.compareTo(10)}'); // Output: -1 (since number < 10)
// remainder() - Returns the remainder
// after division
print('Remainder when divided by 5: ${number.remainder(5)}'); // Output: -2.75
// round() - Rounds the number to
// the nearest integer
print('Rounded Value: ${number.round()}'); // Output: -13
// toDouble() - Converts int to double
print('Integer to Double: ${intNumber.toDouble()}'); // Output: 15.0
// toInt() - Converts double to int
print('Double to Integer: ${number.toInt()}'); // Output: -12
// toString() - Converts number to String
print('Number as String: ${number.toString()}'); // Output: "-12.75"
// truncate() - Removes the decimal
// part, returning an integer
print('Truncated Value: ${number.truncate()}'); // Output: -12
}
Output:
Absolute Value: 12.75
Ceiling Value: -12
Floor Value: -13
Compare To (10): -1
Remainder when divided by 5: -2.75
Rounded Value: -13
Integer to Double: 15
Double to Integer: -12
Number as String: -12.75
Truncated Value: -12
Some try-error for num
Example 1: Using num
to Store Different Number Types
void main(){
int x = 10;
// Error: A value of type 'double'
// can't be assigned to a variable
// of type 'int'.
x = 20.5;
num y = 10;
// No Error
y = 10.5;
}
That means we can assign an int to a double, but assigning a double to an int directly will cause an error. You need to use .toInt().
Example 2: Assigning Values Between int
and double
void main(){
double y = 10; // We can store the int value into the double data type but
// we can't store double value in int type.
y = 10.5;
}
We can store the int value into the double data type but we can't store double value in int type.
Dart uses the int type for whole numbers and the double type for floating-point numbers. The num type serves as the parent class for both int and double, which provides flexibility in number storage. Dart also offers a variety of powerful properties and methods to work with numbers efficiently. You can convert strings to numbers by using the num.parse() method.