Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 438 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to remove trailing zeros using Dart

#11
[user3044484](

[To see links please register here]

)'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);
}
}
```
Reply

#12
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);
});
});
});
}
Reply

#13
// 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
Reply

#14
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
);
}
}
```
Reply

#15
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, '');
```
Reply

#16
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();
}
}
Reply

#17
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

Reply

#18
🪐 "Production ready"

```dart
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;
}
}
```

```bash
23.1250 => 23.12
23.0130 => 23.01
23.1300 => 23.13
23.2000 => 23.2
23.0000 => 23
```

```dart
print(23.1300.removeTrailingZeros) => 23.13
print(23.1300.toMoney) => 23.13₺
```
Reply

#19
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$`

```dart
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:
```bash
1
10
10
10
10.1
10.1
10.01
10.01
```
Reply

#20

```dart

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.04**0** or 0.006**00**

`(\.0+$)` - Match zeros and comma at the end of a decimal number like 1 **.00** or 10‎‎‎‎‎‎‎‎‏‏‎ **.0**
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through