Skip to content

Instantly share code, notes, and snippets.

@abdorll
Last active December 3, 2023 13:51
Show Gist options
  • Save abdorll/18165789edae26136b3da112a6ea63c3 to your computer and use it in GitHub Desktop.
Save abdorll/18165789edae26136b3da112a6ea63c3 to your computer and use it in GitHub Desktop.
double in dart example
// Import the 'dart:math' library to access mathematical functions.
import 'dart:math';
void main() {
// ==================BASIC USAGE
double height = 190.5; // Declare and initialize a double variable with a decimal value.
double weight = 78.2;
double temperature = 25.3;
double? distance; // Nullable double variable.
distance = 42.0;
var someVariable = 72.5; // Dart infers the type as double.
// =================ARITHMETIC OPERATIONS
double x = 10.5;
double y = 5.2;
double sum = x + y; // Add two double values.
double difference = x - y; // Subtract one double from another.
double product = x * y; // Multiply two double values.
double quotient = x / y; // Divide one double by another.
// ==================PROPERTIES
double number = 16.75;
// 1. hashCode → int (Returns a hash code for a numerical value)
number.hashCode; // (Not a property of double alone)
// 2. isFinite → bool (Returns true if this double is finite; otherwise, returns false.)
number.isFinite;
// 3. isInfinite → bool (Returns true if this double is positive infinity or negative infinity; otherwise, returns false.)
number.isInfinite;
// 4. isNegative → bool (Returns true if this double is negative; otherwise, returns false.)
number.isNegative;
// 5. isNaN → bool (Returns true if this double is the special "not-a-number" (NaN) value; otherwise, returns false.)
number.isNaN;
// 6. sign → double (Returns -1.0 if the number is negative, 0.0 if it's zero, and 1.0 if it's positive.)
number.sign;
// 7. runtimeType → Type (A representation of the runtime type of the object.)
number.runtimeType;
// ================METHODS
// 1. abs() → double (Returns the absolute value of this double)
number.abs();
// 2. ceil() → double (Returns the smallest integer value greater than or equal to this double.)
number.ceil();
// 3. floor() → double (Returns the largest integer value less than or equal to this double.)
number.floor();
// 4. truncate() → int (Returns the integer obtained by discarding any fractional part of this number)
number.truncate();
// 5. toStringAsFixed(int fractionDigits) → String (A decimal-point string-representation of this number)
number.toStringAsFixed(2); //2 dp
// 6. round() → int (Returns the integer value closest to this double.)
number.round();
// 7. toString() → String (Returns a string representation of this double.)
number.toString();
// ===============STATIC METHOD
// 6. parse(String source) → double (Parse a string as a double literal and return its value.)
String numString = "123.45";
double parsedDouble = double.parse(numString);
//print(parsedDouble);
// 7. tryParse(String source) → double? (Parse a string as a double literal and return its value, or null if parsing fails.)
String invalidString = "abc";
double? parsedDoubleOrNull = double.tryParse(invalidString);
//print(parsedDoubleOrNull); // This will print 'null' because parsing fails.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment