Working with Numbers
Explore how to declare and use Dart's numeric data types including int for whole numbers, double for decimals, and the versatile num type. Understand when to use each subtype, including hexadecimal and scientific notation. Gain skills to write clear, type-safe numeric assignments and output in Dart.
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 ...