Make printf trim trailing zeroes with %[Gg] format #458
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR modifies the behavior of
pico_printf
to automatically trim trailing zeroes when using the%G
or%g
format specifiers. This is the behavior that is specified by GCC:Previously,
printf("%.14g", 2000)
would output2000.0000000000
, but with this change it outputs just2000
. This is useful for programs that want the minimum-length conversion of a number with the most precision necessary. (My example is Lua, which usessprintf(s, "%.14g", num)
to convert numbers to strings; previously this would result in unnecessarily large conversions for small integers.)This change does not affect the default
printf
- newlib exhibits the same behavior aspico_printf
currently does, so that would have to be adjusted as well (and I can say after reading the code that I'm not the one to do that). Users will have to linkpico_printf
manually to benefit AFAICT.Please let me know if this breaks anything - I didn't have a chance to test this fully (I'm embroiled in debugging some other stuff), but looking at the code I don't think there should be much to break.