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
// 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]);
}void testMissingLeadingZero() { final opacity = .5; final opacity = .5; final scale = .75; final scale = .75; final threshold = .001; final threshold = .001; final value = .999;void testRedundantTrailingZero() { const half = 1.50; const half = 1.50; const tenth = 0.50; const tenth = 0.50; final long = 1.230; final long = 1.230; final doubled = 1.00;class Animation { final double speed = .5; final double speed = .5; final double curve = .25; final double curve = .25; final double trailing = 2.50;double calculateOpacity() { return .8;void processValues() { final list = [.1, .2, .3];void processValues() { final list = [.1, .2, .3];void processValues() { final list = [.1, .2, .3]; final map = { 'x': .5, 'x': .5, 'y': 1.50,void mathOperations() { final result = .5 + .25;void mathOperations() { final result = .5 + .25; final result = .5 + .25; final product = 2.50 * .75; final result = .5 + .25; final product = 2.50 * .75;bool validateRange(double value) { return value > .0;void mixedFormats() { final a = .5; final a = .5; final c = .125;void testContainers(List<double> xs, int count) { final (a, _) = (.5, 0); int _u; (b, _u) = (1.50, 0); lbl: { final c = .25; } for (final (d, _) in [(.75, 0)]) { final e = switch (count) { 0 => .125, 0 => .125, _ => 1.230, }; final f = switch (.5) { }; final g = [if (count > 0) .5]; final g = [if (count > 0) .5]; final h = [...[.5]]; final h = [...[.5]]; final i = (x: .5, y: 0); final i = (x: .5, y: 0); final j = {for (final k in xs) k: .5}; final j = {for (final k in xs) k: .5}; assert(.5 > 0, "x"); assert(.5 > 0, "x"); final l = (.5 as Object) is double;Valid
// 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.
{
"linter": {
"rules": {
"style": {
"double-literal-format": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/double-literal-format: <reason>More style rules
- empty-constructor-bodies
empty_constructor_bodiesrecommendedFlags constructors with an empty block body
{}. - enum-constants-ordering
Flags enum constants whose declared names violate the configured order.
- eol-at-end-of-file
eol_at_end_of_filerecommendedFlags 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-names
file_namesrecommendedEnforces
lowercase_with_underscoresfor source file names. - flutter-style-todos
flutter_style_todosFlags
TODOcomments that do not follow the Flutter style// TODO(username): message, https://URL-to-issue. - format-comment
format-commentFlags comments that are not formatted as complete sentences.
- implicit-call-tearoffs
implicit_call_tearoffsExplicitly tear off callable objects' call methods.