Rules / Style / double-literal-format

double-literal-format

lint/style/double-literal-format
recommended
Same as double-literal-format · Dart Code Metrics

Flags double literals written with redundant or missing digits.

Consistent numeric literals are easier to scan and diff. Three formatting mistakes are reported: a redundant leading zero (05.0), a missing leading zero before the decimal point (.5, which should be 0.5), and a redundant trailing zero in the fractional part (1.50). A literal whose fractional part is exactly 0 (24.0) is deliberately left alone, since stripping that digit would turn the double into an int.

Invalid

example.dartdart
// Test cases for double-literal-format rule
// All violations are marked inline below.

// Missing leading zero: `.5` should be `0.5`.
void testMissingLeadingZero() {
  final opacity = .5;
  final scale = .75;
  final threshold = .001;
  final value = .999;
}

// Redundant trailing zero: the fractional part has a non-"0" digit, so the
// trailing zero is pure noise (`1.50` → `1.5`), and stripping it keeps a double.
void testRedundantTrailingZero() {
  const half = 1.50;
  const tenth = 0.50;
  final long = 1.230;
  final doubled = 1.00;
}

class Animation {
  final double speed = .5;
  final double curve = .25;
  final double trailing = 2.50;
}

double calculateOpacity() {
  return .8;
}

void processValues() {
  final list = [.1, .2, .3];
  final map = {
    'x': .5,
    'y': 1.50,
  };
}

void mathOperations() {
  final result = .5 + .25;
  final product = 2.50 * .75;
}

bool validateRange(double value) {
  return value > .0;
}

void mixedFormats() {
  final a = .5;
  final c = .125;
}
Double literal shouldn't begin with '.'.
5void testMissingLeadingZero() {
6 final opacity = .5;
Double literal shouldn't begin with '.'.
6 final opacity = .5;
7 final scale = .75;
Double literal shouldn't begin with '.'.
7 final scale = .75;
8 final threshold = .001;
Double literal shouldn't begin with '.'.
8 final threshold = .001;
9 final value = .999;
Double literal shouldn't have a trailing '0'.
14void testRedundantTrailingZero() {
15 const half = 1.50;
Double literal shouldn't have a trailing '0'.
15 const half = 1.50;
16 const tenth = 0.50;
Double literal shouldn't have a trailing '0'.
16 const tenth = 0.50;
17 final long = 1.230;
Double literal shouldn't have a trailing '0'.
17 final long = 1.230;
18 final doubled = 1.00;
Double literal shouldn't begin with '.'.
21class Animation {
22 final double speed = .5;
Double literal shouldn't begin with '.'.
22 final double speed = .5;
23 final double curve = .25;
Double literal shouldn't have a trailing '0'.
23 final double curve = .25;
24 final double trailing = 2.50;
Double literal shouldn't begin with '.'.
27double calculateOpacity() {
28 return .8;
Double literal shouldn't begin with '.'.
31void processValues() {
32 final list = [.1, .2, .3];
Double literal shouldn't begin with '.'.
31void processValues() {
32 final list = [.1, .2, .3];
Double literal shouldn't begin with '.'.
31void processValues() {
32 final list = [.1, .2, .3];
Double literal shouldn't begin with '.'.
33 final map = {
34 'x': .5,
Double literal shouldn't have a trailing '0'.
34 'x': .5,
35 'y': 1.50,
Double literal shouldn't begin with '.'.
39void mathOperations() {
40 final result = .5 + .25;
Double literal shouldn't begin with '.'.
39void mathOperations() {
40 final result = .5 + .25;
Double literal shouldn't have a trailing '0'.
40 final result = .5 + .25;
41 final product = 2.50 * .75;
Double literal shouldn't begin with '.'.
40 final result = .5 + .25;
41 final product = 2.50 * .75;
Double literal shouldn't begin with '.'.
44bool validateRange(double value) {
45 return value > .0;
Double literal shouldn't begin with '.'.
48void mixedFormats() {
49 final a = .5;
Double literal shouldn't begin with '.'.
49 final a = .5;
50 final c = .125;

Valid

example.dartdart
// Good cases for double-literal-format rule
// No violations expected

void testCorrectLeadingZero() {
  final opacity = 0.5;
  final scale = 0.75;
  final threshold = 0.001;
  final value = 0.999;
}

void testIntegersAsIntegers() {
  const duration = 1;
  const timeout = 2;
  final ratio = 10;
  final factor = 5;
}

// dcl checks literal *formatting* only. A trailing zero whose fractional part
// is exactly "0" (`1.0`, `24.0`) is NOT flagged: stripping it would turn a
// `double` into an `int`, which is a different value, not a reformat.
void testTrailingSingleZeroIsFine() {
  const duration = 1.0;
  const timeout = 2.0;
  final ratio = 10.0;
  final factor = 5.0;
  final zero = 0.0;
  const big = 100.0;
}

class Animation {
  final double speed = 0.5;
  final double delay = 1.5;
  final double curve = 0.25;
  final double full = 1.0;
}

double calculateOpacity() {
  return 0.8;
}

void processValues() {
  final list = [0.1, 0.2, 0.3];
  final map = {
    'x': 0.5,
    'y': 1.5,
  };
}

void mathOperations() {
  final result = 0.5 + 0.25;
  final product = 2.5 * 0.75;
}

bool validateRange(double value) {
  return value > 0.0;
}

void mixedFormats() {
  final a = 0.5;
  final b = 3.5;
  final c = 0.125;
  final d = 100.5;
}

void properFormatting() {
  const pi = 3.14159;
  final sqrtTwo = 1.414;
  final goldenRatio = 1.618;
}

class Percentage {
  final double value = 0.5;

  bool isValid() => value >= 0.0 && value <= 1;
}

void performanceMetrics() {
  final cpuUsage = 0.85;
  final memoryUsage = 0.92;
  final diskUsage = 0.78;
}

How to configure

Set the severity of double-literal-format in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "double-literal-format": "error"
      }
    }
  }
}