![]() |
VOOZH | about |
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:
👁 n3umdrawio
The int data type is used to represent whole numbers.
int var_name;
Example :
The double data type is used to represent 64-bit floating-point numbers.
double var_name;
Example :
Example Usage :
Output:
2
1.5
Note: The
numis actually the superclass ofintanddouble, meansintanddoubleextendnum.
The parse() function converts a valid numeric string into a number, but if the string is not numeric, it throws a FormatException.
Example :
Output:
Sum = 3.34Property | 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:
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
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 :
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
Example 1: Using num to Store Different Number Types
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
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.