How to remove trailing zeros using Dart

I made regular expression pattern for that feature.

double num = 12.50; // 12.5
double num2 = 12.0; // 12
double num3 = 1000; // 1000

RegExp regex = RegExp(r'([.]*0)(?!.*\d)');

String s = num.toString().replaceAll(regex, '');

UPDATE
A better approach, just use this method:

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

OLD
This meets the requirements:

double x = 12.0;
double y = 12.5;

print(x.toString().replaceAll(RegExp(r'.0'), ''));
print(y.toString().replaceAll(RegExp(r'.0'), ''));

X Output: 12
Y Output: 12.5

Tags:

Dart

Flutter