Format: Fill float printing with '0's until the precision

This commit is contained in:
Riyyi
2024-09-07 16:57:18 +02:00
parent d5bf50715e
commit 243442a3b6
+8 -1
View File
@@ -189,12 +189,19 @@ void Builder::putF64(double number, uint8_t precision) const
// There is no number behind the decimal point // There is no number behind the decimal point
if (dot == std::string::npos) { if (dot == std::string::npos) {
if (precision > 0) { if (precision > 0) {
converted += ".0"; converted += '.' + std::string(precision, '0');
} }
m_builder << converted; m_builder << converted;
return; return;
} }
// If there are less numbers behind the decimal point than the precision
if (converted.length() < length) {
converted += std::string(length - converted.length(), '0');
m_builder << converted;
return;
}
// Only round if there are more numbers behind the decimal point than the precision, // Only round if there are more numbers behind the decimal point than the precision,
// or the number that comes after the maximum precision is higher than 4 // or the number that comes after the maximum precision is higher than 4
if (converted.length() > length && converted[length] > '4') { if (converted.length() > length && converted[length] > '4') {