A numeric literal is either integer(int) or decimal(double) value.
The built-in integer type is to store integer values.
Depending on the platform the dart allows the 64 bits values. On the Dart VM, values can be from -2^63 to 2^63 - 1.
Dart that’s compiled to JavaScript, allowing values from -2^53 to 2^53 - 1.
The following code snippet shows how to declare integer literal.
main() {
// declare integer literal
var x = 1;
print(x);
}
The built-in decimal is to store 64-bit (double-precision) floating-point numbers. If a number includes a decimal, it is a double.
The following code snippet shows how to declare decimal literal.
main() {
// declare double literal
var x = 1.1;
print(x);
}
The int.parse() method is to parse source (input value) as a, possibly signed, integer literal and return its value.
The following code snippet shows how to convert string to integer.
var one = int.parse('1');
print((one == 1)); //true
}
The double.parse() is to parse source as a double literal and return its value.
The following code snippet shows how to convert string to double.
var onePointOne = double.parse('1.1');
print((onePointOne == 1.1)); //true
The toString() is to return a string representation of the source.
The following code snippet shows how to convert int to string.
int source = 123;
var target = source.toString();
print((target == '123')); //true
The toStringAsFixed() method is to represent a string by specifying exactly fraction digits after the decimal point.
The following code snippet shows how to convert a double into a string, specifying two digits to the right of the decimal.
String piAsString = 3.14159.toStringAsFixed(2);
print((piAsString == '3.14')); //true
The purpose of the toStringAsPrecision() method is to represent strings with exactly precision significant digits to return.
The following code snippet shows how to convert a double into a String, with exactly precision significant digits to return.
String piPrecision = 123.456.toStringAsPrecision(2);
print(piPrecision); // 1.2e+2
print(double.parse(piPrecision)); // 120.0
Both int and double are subtypes of num.
The num type includes basic operators such as +, -, /, and *
main() {
int x = 12;
print(x / 2); //Divide
print(x + 2); // Add
print(x - 2); // Subtract
print(x * 2); // Multiply
print(x % 5); // modulo
print(x ~/ 5); // Divide, returning an integer result
}