make dec formatting consistent with f64

This commit is contained in:
Folkert 2023-09-11 20:21:51 +02:00
parent 3560498106
commit 59af059912
No known key found for this signature in database
GPG Key ID: 1F17F6FFD112B97C

View File

@ -384,14 +384,19 @@ impl RocDec {
// push a dummy character so we have space for the decimal dot
string.push('$');
// Safety: at any time, the string only contains ascii characters, so it is always valid utf8
let bytes = unsafe { string.as_bytes_mut() };
if decimal_location == last_nonzero_byte {
// never have a '.' as the last character
string.truncate(last_nonzero_byte)
} else {
// Safety: at any time, the string only contains ascii characters, so it is always valid utf8
let bytes = unsafe { string.as_bytes_mut() };
// shift the fractional part by one
bytes.copy_within(decimal_location..last_nonzero_byte, decimal_location + 1);
// shift the fractional part by one
bytes.copy_within(decimal_location..last_nonzero_byte, decimal_location + 1);
// and put in the decimal dot in the right place
bytes[decimal_location] = b'.';
// and put in the decimal dot in the right place
bytes[decimal_location] = b'.';
}
string.as_str()
}