Dart Number
The data type used to store the numerical value is called Number. It might be one of two kinds in dart:
- Integer
- Double
Dart integer: Whole numbers that may be expressed without a fractional part are known as integers. Take -20, 30, -3215, 0, etc. as examples. A signed or unsigned integer can exist. The integer value is represented by a range of non-decimal values, from -263 to 263. In Dart, an integer value is declared using the int keyword.
int id = 501;
Dart Double: The Double numbers are those that have larger decimal points or may be represented in floating-point notation. In Dart, a Double value is declared with the double keyword.
double root = 1.41234;
or
double rupees = 100000;
Rules for the integer value
- The value of an integer must be a digit.
- An integer number shouldn’t contain decimal points.
- Positive numbers are invariably unsigned. Positive or negative numbers are both possible.
- Although the size of the integer value varies depending on the platform, it should not be more than 64 bits.
Let’s examine the following illustration:
Example
void main(){
int r = 5;
double pi = 3.14;
double res = 4*pi*r*r;
print("The area of sphere = ${(res)}");
}
Output
The area of sphere 314
Dart parse() function
The numeric string is changed to a number using the parse() method. Take a look at this example:
Example
void main(){
var a = num.parse("20.56");
var b = num.parse("15.63");
var c = a+b;
print("The sum is = ${c}");
}
Output
The sum is = 36.19
Explanation
In the example above, we used the parse() method to transform the numeric strings into numbers, which were then put in the variables. Following the conversion’s successful completion, we added the result and displayed it on the screen.
Number Properties
| Properties | Description |
|---|---|
| hashcode | It returns the hash code of the given number. |
| isFinite | If the given number is finite, then it returns true. |
| isInfinite | If the number infinite it returns true. |
| isNan | If the number is non-negative then it returns true. |
| isNegative | If the number is negative then it returns true. |
| sign | It returns -1, 0, or 1 depending upon the sign of the given number. |
| isEven | If the given number is an even then it returns true. |
| isOdd | If the given number is odd then it returns true. |
Number Methods
The following lists the frequently used numbering techniques.
| Method | Description |
|---|---|
| abs() | It gives the absolute value of the given number. |
| ceil() | It gives the ceiling value of the given number. |
| floor() | It gives the floor value of the given number. |
| compareTo() | It compares the value with other number. |
| remainder() | It gives the truncated remainder after dividing the two numbers. |
| round() | It returns the round of the number. |
| toDouble() | It gives the double equivalent representation of the number. |
| toInt() | Returns the integer equivalent representation of the number. |
| toString() | Returns the String equivalent representation of the number |
| truncate() | Returns the integer after discarding fraction digits. |