Dart – NumberFormat

Actually, I think it’s easier to go with truncateToDouble() and toStringAsFixed() and not use NumberFormat at all:

n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 2);

So for example:

main() {
  double n1 = 15.00;
  double n2 = 15.50;

  print(format(n1));
  print(format(n2));
}

String format(double n) {
  return n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 2);
}

Prints to console:

15
15.50

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)