double-literal-format

Same as double-literal-format in Dart Code Metrics

Group
Style
Path
lint/style/double-literal-format
Default
Off, opt in
Origin
Dart Code Metrics
Rule
227 of 512

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;
}

// Regression: a literal must be found wherever it appears, including inside
// Dart 3 containers whose walkers were previously not visited.
void testContainers(List<double> xs, int count) {
  final (a, _) = (.5, 0);
  double b;
  int _u;
  (b, _u) = (1.50, 0);
  lbl: {
    final c = .25;
    print(c);
  }
  for (final (d, _) in [(.75, 0)]) {
    print(d);
  }
  final e = switch (count) {
    0 => .125,
    _ => 1.230,
  };
  final f = switch (.5) {
    _ => 0,
  };
  final g = [if (count > 0) .5];
  final h = [...[.5]];
  final i = (x: .5, y: 0);
  final j = {for (final k in xs) k: .5};
  assert(.5 > 0, "x");
  final l = (.5 as Object) is double;
  print([a, b, e, f, g, h, i, j, l]);
}
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;
Double literal shouldn't begin with `.`.
55void testContainers(List<double> xs, int count) {
56 final (a, _) = (.5, 0);
Double literal shouldn't have a trailing `0`.
58 int _u;
59 (b, _u) = (1.50, 0);
Double literal shouldn't begin with `.`.
60 lbl: {
61 final c = .25;
Double literal shouldn't begin with `.`.
63 }
64 for (final (d, _) in [(.75, 0)]) {
Double literal shouldn't begin with `.`.
67 final e = switch (count) {
68 0 => .125,
Double literal shouldn't have a trailing `0`.
68 0 => .125,
69 _ => 1.230,
Double literal shouldn't begin with `.`.
70 };
71 final f = switch (.5) {
Double literal shouldn't begin with `.`.
73 };
74 final g = [if (count > 0) .5];
Double literal shouldn't begin with `.`.
74 final g = [if (count > 0) .5];
75 final h = [...[.5]];
Double literal shouldn't begin with `.`.
75 final h = [...[.5]];
76 final i = (x: .5, y: 0);
Double literal shouldn't begin with `.`.
76 final i = (x: .5, y: 0);
77 final j = {for (final k in xs) k: .5};
Double literal shouldn't begin with `.`.
77 final j = {for (final k in xs) k: .5};
78 assert(.5 > 0, "x");
Double literal shouldn't begin with `.`.
78 assert(.5 > 0, "x");
79 final l = (.5 as Object) is double;

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;
}

Configure double-literal-format

Set its severity in falcon.json. See the configuration guide.

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

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/double-literal-format: <reason>
  • empty-constructor-bodiesempty_constructor_bodiesrecommended

    Flags constructors with an empty block body {}.

  • enum-constants-ordering

    Flags enum constants whose declared names violate the configured order.

  • eol-at-end-of-fileeol_at_end_of_filerecommended

    Flags a non-empty file that does not end with exactly one line break.

  • extend-equatableequatable

    Require Equatable semantics for configured class/file name patterns.

  • file-namesfile_namesrecommended

    Enforces lowercase_with_underscores for source file names.

  • flutter-style-todosflutter_style_todos

    Flags TODO comments that do not follow the Flutter style // TODO(username): message, https://URL-to-issue.

  • format-commentformat-comment

    Flags comments that are not formatted as complete sentences.

  • implicit-call-tearoffsimplicit_call_tearoffs

    Explicitly tear off callable objects' call methods.