I would like the optimal solution for removing trailing zeros using Dart. If I have a double that is 12.0 it should output 12. If I have a double that is 12.5 it should output 12.5
21 Answers
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, '');
-
3This works for all scenarios. The toStringAsFixed(n.truncateToDouble() method in my answer auto rounds decimal place based values to 1 when they are less than 1 (i.e. 0.95) which is an unwanted side effect. – John Mar 15 '19 at 23:12
-
Why didn't you use the regex you've declared and assiged? It's this part: `RegExp regex = RegExp(r"([.]*0)(?!.*\d)");` Instead, you've given a new RegExp into `replaceAll`. – SametSahin Dec 30 '20 at 18:16
-
2`double numx = 12.500; ` return value is 12.50; – alexwan02 Jan 05 '21 at 08:00
-
1RegExp(r'\.0') is better for me – temirbek May 11 '21 at 11:39
-
8@alexwan02 use `RegExp(r"([.]*0+)(?!.*\d)")` to remove all trailing zeroes after the decimal point. – Oleksii Shliama May 30 '21 at 02:26
-
6@Yuna Could you check your answer again? When you pass 1000, you'll get 100. Why? – RuslanBek Jan 19 '22 at 09:38
-
2@Ruslanbek0809, try RegExp(r'(\.[0]+)') – Fəqan Çələbizadə Feb 04 '22 at 14:34
-
6Be careful - I confirmed that the REGEX in the accepted answer turns 1000 into 100 despite the commented code saying otherwise. The Regex above my comment does not work properly either @FəqanÇələbizadə - it turns 1000.05 into 10005 – JakeD Apr 05 '22 at 21:15
-
1This approach has flaws, don't use for prices (1000 -> 100) – Kirill Karmazin Apr 19 '22 at 12:11
-
This writes 1 instead of 10 – gurnisht Jul 21 '22 at 07:38
-
This is incorrect answer, please down vote this. Following this resulted into major problem in my app. – Ashish Khurange Aug 22 '22 at 06:37
-
This converts 10.0 to 1. So don't use it – Muhammad Umair Saqib Aug 06 '23 at 07:22
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

- 2,551
- 3
- 17
- 29
Use NumberFormat:
String formatQuantity(double v) {
if (v == null) return '';
NumberFormat formatter = NumberFormat();
formatter.minimumFractionDigits = 0;
formatter.maximumFractionDigits = 2;
return formatter.format(v);
}

- 1,727
- 16
- 18
-
1be careful because this approach from`4.003000` will give you `4` – Kirill Karmazin Apr 19 '22 at 12:02
-
Lots of the answers don't work for numbers with many decimal points and are centered around monetary values.
To remove all trailing zeros regardless of length:
removeTrailingZeros(String n) {
return n.replaceAll(RegExp(r"([.]*0+)(?!.*\d)"), "");
}
Input: 12.00100003000
Output: 12.00100003
If you only want to remove trailing 0's that come after a decimal point, use this instead:
removeTrailingZerosAndNumberfy(String n) {
if(n.contains('.')){
return double.parse(
n.replaceAll(RegExp(r"([.]*0+)(?!.*\d)"), "") //remove all trailing 0's and extra decimals at end if any
);
}
else{
return double.parse(
n
);
}
}

- 1,503
- 8
- 17
-
so far this answer with `removeTrailingZerosAndNumberfy()` method is the best approach if you need to show prices on UI – Kirill Karmazin Apr 19 '22 at 12:24
-
If what you want is to convert a double without decimals to an int but keep it as a double if it has decimals, I use this method:
num doubleWithoutDecimalToInt(double val) {
return val % 1 == 0 ? val.toInt() : val;
}

- 365
- 3
- 11
I found another solution, to use num
instead of double
. In my case I'm parsing String to num:
void main() {
print(num.parse('50.05').toString()); //prints 50.05
print(num.parse('50.0').toString()); //prints 50
}

- 393
- 5
- 13
-
2
-
1Does not work for me when running dart from terminal. `num.parse('10.0').toString()` returns 10.0. I tried in DartPad and then it returned 10 however. – Simon Bengtsson Aug 20 '22 at 14:37
Here is what I've come up with:
extension DoubleExtensions on double {
String toStringWithoutTrailingZeros() {
if (this == null) return null;
return truncateToDouble() == this ? toInt().toString() : toString();
}
}
void main() {
group('DoubleExtensions', () {
test("toStringWithoutTrailingZeros's result matches the expected value for a given double",
() async {
// Arrange
final _initialAndExpectedValueMap = <double, String>{
0: '0',
35: '35',
-45: '-45',
100.0: '100',
0.19: '0.19',
18.8: '18.8',
0.20: '0.2',
123.32432400: '123.324324',
-23.400: '-23.4',
null: null
};
_initialAndExpectedValueMap.forEach((key, value) {
final initialValue = key;
final expectedValue = value;
// Act
final actualValue = initialValue.toStringWithoutTrailingZeros();
// Assert
expect(actualValue, expectedValue);
});
});
});
}

- 1,182
- 2
- 12
- 20
-
great answer, it works even if there are more than one trailing zeros. Thanks. – Mol0ko Mar 09 '21 at 07:11
String removeTrailingZero(String string) {
if (!string.contains('.')) {
return string;
}
string = string.replaceAll(RegExp(r'0*$'), '');
if (string.endsWith('.')) {
string = string.substring(0, string.length - 1);
}
return string;
}
======= testcase below =======
000 -> 000
1230 -> 1230
123.00 -> 123
123.001 -> 123.001
123.00100 -> 123.001
abc000 -> abc000
abc000.0000 -> abc000
abc000.001 -> abc000.001

- 463
- 5
- 7
To improve on What @John's answer: here is a shorter version.
String formatNumber(double n) {
return n.toStringAsFixed(0) //removes all trailing numbers after the decimal.
}

- 869
- 1
- 12
- 19
This function removes all trailing commas. It also makes it possible to specify a maximum number of digits after the comma.
extension ToString on double {
String toStringWithMaxPrecision({int? maxDigits}) {
if (round() == this) {
return round().toString();
} else {
if (maxDigits== null) {
return toString().replaceAll(RegExp(r'([.]*0)(?!.*\d)'), "");
} else {
return toStringAsFixed(maxDigits)
.replaceAll(RegExp(r'([.]*0)(?!.*\d)'), "");
}
}
}
}
//output without maxDigits:
// 1.0 -> 1
// 1.0000 -> 1
// 0.99990 -> 0.9999
// 0.103 -> 0.103
//
////output with maxDigits of 2:
// 1.0 -> 1
// 1.0000 -> 1
// 0.99990 -> 0.99
// 0.103 -> 0.1

- 31
- 3
Here is a very simple way. Using if else I will check if the number equals its integer or it is a fraction and take action accordingly
num x = 24/2; // returns 12.0
num y = 25/2; // returns 12.5
if (x == x.truncate()) {
// it is true in this case so i will do something like
x = x.toInt();
}

- 31
- 6
"Production ready"
extension MeineVer on double {
String get toMoney => '$removeTrailingZeros₺';
String get removeTrailingZeros {
// return if complies to int
if (this % 1 == 0) return toInt().toString();
// remove trailing zeroes
String str = '$this'.replaceAll(RegExp(r'0*$'), '');
// reduce fraction max length to 2
if (str.contains('.')) {
final fr = str.split('.');
if (2 < fr[1].length) {
str = '${fr[0]}.${fr[1][0]}${fr[1][1]}';
}
}
return str;
}
}
23.1250 => 23.12
23.0130 => 23.01
23.1300 => 23.13
23.2000 => 23.2
23.0000 => 23
print(23.1300.removeTrailingZeros) => 23.13
print(23.1300.toMoney) => 23.13₺

- 608
- 5
- 18
user3044484's version with Dart extension:
extension StringRegEx on String {
String removeTrailingZero() {
if (!this.contains('.')) {
return this;
}
String trimmed = this.replaceAll(RegExp(r'0*$'), '');
if (!trimmed.endsWith('.')) {
return trimmed;
}
return trimmed.substring(0, this.length - 1);
}
}

- 1
- 1
-
Last line should be `return trimmed.substring(0, trimmed.length - 1);` – tudorprodan Nov 04 '20 at 14:29
// The syntax is same as toStringAsFixed but this one removes trailing zeros
// 1st toStringAsFixed() is executed to limit the digits to your liking
// 2nd toString() is executed to remove trailing zeros
extension Ex on double {
String toStringAsFixedNoZero(int n) =>
double.parse(this.toStringAsFixed(n)).toString();
}
// It works in all scenarios. Usage
void main() {
double length1 = 25.001;
double length2 = 25.5487000;
double length3 = 25.10000;
double length4 = 25.0000;
double length5 = 0.9;
print('\nlength1= ' + length1.toStringAsFixedNoZero(3));
print('\nlength2= ' + length2.toStringAsFixedNoZero(3));
print('\nlenght3= ' + length3.toStringAsFixedNoZero(3));
print('\nlenght4= ' + length4.toStringAsFixedNoZero(3));
print('\nlenght5= ' + length5.toStringAsFixedNoZero(0));
}
// output:
// length1= 25.001
// length2= 25.549
// lenght3= 25.1
// lenght4= 25
// lenght5= 1

- 19
- 2
you can do a simple extension on the double class and add a function which in my case i called it neglectFractionZero()
in this extension function on double(which returns a string) i split the converted number to string and i check if the split part of the string is "0" , if so i return the first part only of the split and i neglect this zero
you can modify it according to your needs
extension DoubleExtension on double { String neglectFractionZero() {
return toString().split(".").last == "0"? toString().split(".").first:toString();
}
}

- 71
- 3
-
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Feb 22 '22 at 00:42
The toString() already removes the useless "0" unless it is "1.0". So you just have to remove ".0" if it ends with that.
regex \.0$
for (var e in <double>[
1,
10,
10.0,
10.00,
10.1,
10.10,
10.01,
10.010,
]) {
print(e.toString().replaceAll(RegExp(r'\.0$'), ''));
}
result:
1
10
10
10
10.1
10.1
10.01
10.01

- 1,263
- 11
- 24
String formatWithoutTrailingZeros(double n) {
return n.toString().replaceFirst(RegExp(r'(?<=\.\d*)(0+$)|(\.0+$)'), '');
}
Explenation:
(?<=\.\d*)(0+$)
- Match zeros at the end of a decimal number like 1.040 or 0.00600
(\.0+$)
- Match zeros and comma at the end of a decimal number like 1 .00 or 10 .0

- 4,215
- 3
- 28
- 35
void main(List<String> args) {
print(f('0'));
print(f('0.0'));
print(f('12'));
print(f('12.0'));
print(f('12.5'));
print(f('12.5000'));
}
String f(String s) {
var sepCount = 0;
return switch (String.fromCharCodes(s.codeUnits.reversed
.skipWhile((c) => c == 0x30)
.skipWhile((c) => c == 0x2e && sepCount++ == 0)
.toList()
.reversed)) {
'' => '0',
final r => r,
};
}
Output:
0
0
12
12
12.5
12.5

- 10,684
- 4
- 32
- 54
One way is good, but 25 ways is better.
Another way to remove trailing zeros.
import 'package:parser_combinator/parser/delimited.dart';
import 'package:parser_combinator/parser/digit1.dart';
import 'package:parser_combinator/parser/eof.dart';
import 'package:parser_combinator/parser/not.dart';
import 'package:parser_combinator/parser/preceded.dart';
import 'package:parser_combinator/parser/replace_all.dart';
import 'package:parser_combinator/parser/skip_while1.dart';
import 'package:parser_combinator/parser/tag.dart';
import 'package:parser_combinator/parser/terminated.dart';
import 'package:parser_combinator/parsing.dart';
void main(List<String> args) {
final list = [
'',
'0',
'0.0',
'10.0000000000000000000',
'10.0000000000000000001'
];
for (final element in list) {
print(convert1(element));
print(convert2(element));
}
}
String convert1(String s) {
const trailingZeros = Terminated(
Preceded(
Tag('.'),
SkipWhile1(_isZero),
),
Eof(),
);
const p = ReplaceAll(trailingZeros, _replace);
return parseString(p.parse, s);
}
String convert2(String s) {
const trailingZeros = Delimited(
Tag('.'),
SkipWhile1(_isZero),
Not(Digit1()),
);
const p = ReplaceAll(trailingZeros, _replace);
return parseString(p.parse, s);
}
bool _isZero(int c) => c == 0x30;
String _replace(String s) => '';
Output:
0
0
0
0
10
10
10.0000000000000000001
10.0000000000000000001

- 10,684
- 4
- 32
- 54
I've came up with improved version of @John.
static String getDisplayPrice(double price) {
price = price.abs();
final str = price.toStringAsFixed(price.truncateToDouble() == price ? 0 : 2);
if (str == '0') return '0';
if (str.endsWith('.0')) return str.substring(0, str.length - 2);
if (str.endsWith('0')) return str.substring(0, str.length -1);
return str;
}
// 10 -> 10
// 10.0 -> 10
// 10.50 -> 10.5
// 10.05 -> 10.05
// 10.000000000005 -> 10

- 1,265
- 2
- 17
- 27
void main() {
double x1 = 12.0;
double x2 = 12.5;
String s1 = x1.toString().trim();
String s2 = x2.toString().trim();
print('s1 is $s1 and s2 is $s2');
}
try trim method https://api.dartlang.org/stable/2.2.0/dart-core/String/trim.html

- 2,410
- 4
- 17
- 36