Show a number to two decimal places in Dart (Flutter)

Last updated on January 24, 2022 A Goodman Loading... Post a comment

To round a double number to two decimal places in Dart and Flutter, you can use the toStringAsFixed() method.

Example

The code:

import 'package:flutter/foundation.dart';

void main() {
  const number1 = 1.233;
  const number2 = 4.357;
  const number3 = 10.994;

  if (kDebugMode) {
    print(number1.toStringAsFixed(2));
    print(number2.toStringAsFixed(2));
    print(number3.toStringAsFixed(2));
  }
}

Output:

1.23
4.36
10.99

Further reading:

You can also check out our Flutter category page or Dart category page for the latest tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles