removeInsignificantZeros static method
Removes any zeros at the end of a number string that follow a decimal point (except for one that immediately follows the decimal point).
Implementation
static String removeInsignificantZeros(String str) {
if (str.isNotEmpty != true) return str;
final dotIndex = str.indexOf('.');
if (dotIndex == -1) return str;
final eIndex = str.toLowerCase().indexOf('e');
final decimalCount =
eIndex == -1 ? str.length - dotIndex - 1 : eIndex - dotIndex - 1;
if (decimalCount < 2) return str;
final lastDigitIndex = eIndex == -1 ? str.length - 1 : eIndex - 1;
int endIndex;
for (endIndex = lastDigitIndex; endIndex > dotIndex + 1; endIndex--) {
if (str.substring(endIndex, endIndex + 1) != '0') break;
}
return eIndex == -1
? str.substring(0, endIndex + 1)
: '${str.substring(0, endIndex + 1)}${str.substring(eIndex)}';
}