Rules for Integers
Let us understand the need for the rules with an example given below.
Code:
void main() {
int n1 = 15; // declare an integer
int n2 = 15.50; // declare a double value
// printinting n1 and n2
print(n1);
print(n2);
}
This would surely throw an error since we are declaring 15.50 as int type(which is actually a double type value).
So, These are the following rules for the integer value to keep in mind:
- A digit must be present in an integer value.
- In an integer number, the decimal points should not be included.
- Unsigned numbers will always be a positive number, and a Number can either be negative or positive.
- The size of the integer value is determined by the platform, although it should not exceed 64 bits.
Parsing
The parse() static method converts(parses) a string with numeric literals to a number. The same can be seen in the illustration below.
Code:
void main(){
var n1 = num.parse("15.51");
var n2 = num.parse("27.72");
var x = n1 * n2;
print("The product is = ${x}");
}
Output:
The product is = 429.94
As we can clearly see in this above mentioned example, We used the parse() method to transform the numeric strings into numbers, which we then placed in variables n1 and n2, respectively. We did a multiplication(*) operation and print the output to the screen after the successful conversion.
Note: If any value other than numerals is supplied to the parse() function, it will throw a FormatException.
Dart Numbers: Properties
The properties supported by Dart numbers are listed below.
- hashcode: It returns the supplied number's hash code.
- isFinite: It returns true if the given number is finite.
- isInfinite: This will return true if the number is infinite.
- isNan: This will return true if the number is not negative.
- isNegative: It returns true if the number is negative.
- sign: Depending on the sign of the given number, it returns -1, 0, or 1.
- isEven: It returns true if the given number is an even number.
- isOdd: It returns true if the given number is odd.
Dart Numbers: Methods
The most regularly used methods of number are listed below:
-
abs(): abs in C++ will be used to return the absolute value of a number.
- ceil(): It is used to return the least integer no smaller than the number.
- floor(): It returns the supplied number's floor value.
- compareTo(): It makes a comparison between the value and another number.
- remainder(): After dividing the two numbers, it returns the shortened remainder.
- round(): This method is used to return the round of the number.
- toDouble(): It gives the number's double equivalent representation.
- toInt(): Returns the number's integer equivalent form.
- toString(): The String representation of the number is returned.
- truncate(): After removing fraction digits, returns the integer.
Frequently Asked Questions
In Dart, what is the range of Javascript numbers?
Numbers can have values ranging from -2^63 to 2^3-1. However, because Dart is transformed to javascript directly, it only supports values in the range of -2^53 to 2^53-1.
In Dart, what is a Rune?
Strings in the Dart programming language are just a collection of UTF-16 (16-bit Unicode Transformation Format) code units. Every digit, letter, or symbol is represented by a unique numeric character in the Unicode standard. Any Unicode code point can be represented by a rune, which is an integer.
What is NUM in flutter, exactly?
Attempting to extend or implement num with a type other than int or double is a compile-time error.
Conclusion
In this article, we learned about Dart Numbers, their type, their methods, properties, and rules for declaration and parsing.
You can head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build future applications.
To study more about data types, refer to Abstract Data Types in C++.
We hope this article has helped you enhance your knowledge of Dart programming and Input-Output in it. If you want to learn more, check out our Programming Fundamentals and Competitive Programming articles. Do upvote this article to help other ninjas grow.