In this short article, we are going to learn how to round a number to n decimal places in Dart/Flutter. Or in other words, we format a number to a decimal number with N digits after decimal point.
Let’s go directly into the demo because there are no special things to explain 🧑🎄 🧑🎄 🧑🎄
void main() {
double number = double.parse(1.2345.toStringAsFixed(2));
print(number); // 1.23
number = double.parse(1.2356.toStringAsFixed(2));
print(number); // 1.24
number = double.parse((-1.2345).toStringAsFixed(2));
print(number); // -1.23
number = double.parse((-1.2356).toStringAsFixed(2));
print(number); // -1.24
}
Creator of Coflutter.
Founder & CTO at Bumbii Technology
Founder at Bumbii K12
Follow him on Twitter, Github, StackOverflow, LinkedIn, Upwork.
Thanks!