Hello friends, First of all Happy New year. I know it’s been late but I was busy then. Now let’s come to the main point, In today’s tutorial we would learn about Dart Math inbuilt functions round(), roundToDouble(). We would also discuss another method in which If user creates Float number using var declaration. Then it does not properly works as a Double then above round and round to double methods will not work on it. Now to work it properly we have to convert first it into string and setting up 2 Decimal after point then again convert it into double. I have explained all the work here in this tutorial so you don’t have to worry about it. So in this tutorial we would learn about Round A Double Number in Flutter DART After Decimal.
Contents in this project Round A Double Number in Flutter DART After Decimal :-
1. Open your project’s main.dart file and import material.dart package.
1 |
import 'package:flutter/material.dart'; |
2. Creating void main runApp() method here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Creating MyApp extends StatelessWidget, our main class.
1 2 3 4 5 |
class MyApp extends StatelessWidget { } |
4. Creating 2 Double float variables using double and var syntax. We would convert them one by one in a function.
1 2 3 4 5 |
// Float Double Value double value = 12.288885679948852661; // String Type Value. var price = 99.585174884; |
5. Creating a function named as roundNumbers(). In this function first we would use the round() method to convert the number to it’s closest integer value in round format. In next step we would use the roundToDouble() method to convert the number to its closest double number. We would also covnerting the var variable to string then in Double.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
void roundNumbers() { print(value.round()); print(value.roundToDouble()); // Converting to String. String temp = price.toStringAsFixed(2); // Converting to Double double holder = double.parse(temp); print(holder); } |
6. Creating Widget Build area -> here we would make 1 Button widget and call the above function on onPress event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Round Numbers in Flutter Dart')), body: Center( child: Center( child: Container( margin: const EdgeInsets.all(8), child: ElevatedButton( style: ElevatedButton.styleFrom( primary: Colors.lightBlue[900], padding: const EdgeInsets.all(6), textStyle: const TextStyle(fontSize: 20), ), child: const Text('Click Here To Round Numbers'), onPressed: roundNumbers, ), ), ), ))); } |
7. Complete source code for main.dart file :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // Float Double Value double value = 12.288885679948852661; // String Type Value. var price = 99.585174884; void roundNumbers() { print(value.round()); print(value.roundToDouble()); // Converting to String. String temp = price.toStringAsFixed(2); // Converting to Double double holder = double.parse(temp); print(holder); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Round Numbers in Flutter Dart')), body: Center( child: Center( child: Container( margin: const EdgeInsets.all(8), child: ElevatedButton( style: ElevatedButton.styleFrom( primary: Colors.lightBlue[900], padding: const EdgeInsets.all(6), textStyle: const TextStyle(fontSize: 20), ), child: const Text('Click Here To Round Numbers'), onPressed: roundNumbers, ), ), ), ))); } } |
Screenshot :-
