Table of contents
1.
Introduction
2.
Dart Number
2.1.
Dart Integer
2.2.
Dart Double
3.
Rules for Integers
4.
Parsing
5.
Dart Numbers: Properties
6.
Dart Numbers: Methods
7.
Frequently Asked Questions
7.1.
In Dart, what is the range of Javascript numbers?
7.2.
In Dart, what is a Rune?
7.3.
What is NUM in flutter, exactly?
8.
Conclusion
Last Updated: Mar 27, 2024

Dart Numbers

Author Akshit Pant
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Dart is a client-oriented programming language that may be used to develop apps for various platforms. It is object-oriented and may be compiled into JavaScript or native code.

In this blog, we will be learning about Dart numbers, their type, methods & properties, parsing, and rules.

Before learning how we do so, learning the basic Introduction of Dart is recommended.

So, let us start reading about numbers in Dart.

Dart Number

The numeric values are stored in the Number data type. The following numerical data types are supported by Dart: 

  • Dart Integer
  • Dart Double.

Dart Integer

Whole numbers are stored in Dart Integers. The Dart integer data type is used to represent non-decimal numbers in the range of -263 to 263 – 1. A signed or unsigned integer value can be stored in an integer. The int keyword can be used to declare integers.

Syntax:

int var_name;

Dart Double

A 64-bit (double-precision) floating-point value or a number with more decimal points is represented by a Dart double. The keyword double can be used to declare a double.

Syntax:

double distance = 5.41484

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.

Live masterclass