diff --git a/crates/roc_std/src/lib.rs b/crates/roc_std/src/lib.rs index f8877cae10..219652cbd9 100644 --- a/crates/roc_std/src/lib.rs +++ b/crates/roc_std/src/lib.rs @@ -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() }