22.1 C
New York
Friday, August 19, 2022

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

- Advertisement -

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

Example

The code:Advertisements

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.

Advertisements
- Advertisement -

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles