Skip to content

Instantly share code, notes, and snippets.

@hectorAguero
Last active May 13, 2025 20:32
Show Gist options
  • Save hectorAguero/7e671eb5c343b1a1d20403e36aa708c8 to your computer and use it in GitHub Desktop.
Save hectorAguero/7e671eb5c343b1a1d20403e36aa708c8 to your computer and use it in GitHub Desktop.
Flutter decimal text rendering for doubles
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final one = 1.0;
final oneHalf = 1.5;
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('$one'),
Text('${one.toCleanedValue()}'),
Text('${oneHalf}'),
Text('${oneHalf.toCleanedValue()}'),
Text('${2.0}'),
Text('${2.0.toCleanedValue()}'),
],
),
),
),
);
}
}
extension IntegerConverterExtension on double {
num toCleanedValue() {
if (this == toInt()) {
return toInt();
}
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment