Working with Numbers
Explore how to work with Dart's core numeric types by mastering the use of int for whole numbers, double for decimals and scientific notation, and num for flexible number handling. Understand automatic conversions and best practices for type-safe code.
We'll cover the following...
The most common data types we work with in programming are numbers. If we want a variable to hold a numeric value, we can declare it using the general num data type.
Let us start by creating a simple variable using the num type and assigning a basic number literal to it.
Line 2: We use the flexible
numdata type to reserve memory for a numeric value. We name this containerageand store the whole number25inside it, though thenumtype is versatile enough to also hold decimal values.Line 3: We retrieve the data currently stored in the
agevariable and output it to the console using theprintfunction.
Subtypes of numbers
While num is useful as a general container, Dart numbers are further divided into two specific ...