Fixed GH-12237: sprintf/printf no longer produces negative zero#21273
Open
lacatoire wants to merge 2 commits intophp:masterfrom
Open
Fixed GH-12237: sprintf/printf no longer produces negative zero#21273lacatoire wants to merge 2 commits intophp:masterfrom
lacatoire wants to merge 2 commits intophp:masterfrom
Conversation
When a negative floating-point value rounds to zero at the requested precision, sprintf/printf now outputs "0.00" instead of "-0.00". This aligns the behavior of sprintf() with number_format(), which already suppresses the negative sign for values that round to zero.
mvorisek
reviewed
Feb 22, 2026
Co-authored-by: Michael Voříšek <mvorisek@mvorisek.cz>
Member
|
This looks like a BC break, isn't it? Also, tests are failing |
Member
If this restores previous behaviour that was unintentionally changed, then it is a regression bugfix. |
Member
|
Ah yes I missed that part, sorry! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When a negative floating-point value rounds to zero at the requested precision,
sprintf/printfnow outputs"0.00"instead of"-0.00".This aligns the behavior of
sprintf()/printf()withnumber_format(), which already suppresses the negative sign for values that round to zero.Fix
After
php_conv_fp()formats the number, ifis_negativeis set, we check whetherround(fabs(number) * 10^precision) / 10^precision == 0.0. If so, we clear the negative flag.The check is only applied to
%f/%Fformats (for%e/%E, a non-zero value never formats as zero).