Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ lessThan : < .
thanOrEqual : <= .
greaterThan : > .
greaterOrEqual : >= .
removeTrailingZeros : get string By value double, and remove redundant trailing zeros.
```

* ObjectUtil -> [Example](https://github.com/Sky24n/flutter_wanandroid/blob/master/lib/demos/object_util_page.dart)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ lessThan : < .
thanOrEqual : <= .
greaterThan : > .
greaterOrEqual : >= .
removeTrailingZeros : double转数字字符串,并移除末尾多余的0.
```

* ObjectUtil -> [Example](https://github.com/Sky24n/flutter_wanandroid/blob/master/lib/demos/object_util_page.dart)
Expand Down
18 changes: 18 additions & 0 deletions lib/src/num_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,22 @@ class NumUtil {
static bool greaterOrEqualDecStr(String a, String b) {
return Decimal.parse(a) >= Decimal.parse(b);
}

/// Remove trailing zeros of double number.
static String removeTrailingZeros(double number) {
if (number == null) {
return null;
}

if (number.truncateToDouble() == number) {
return number.toStringAsFixed(0);
}

final value = number.toString();
int index = value.length;

while (value[--index] == '0') {}

return number.toStringAsFixed(index - value.indexOf('.'));
}
}