Bug 18838 – Formatting the number zero with separator doesn't obey width specifier
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-05-07T04:45:38Z
Last change time
2020-03-21T03:56:42Z
Keywords
pull
Assigned to
No Owner
Creator
Tornchi
Comments
Comment #0 by tornchi — 2018-05-07T04:45:38Z
The following code shows the problem. The width specifier is obeyed in each case except when printing the number zero.
import std.stdio;
void main()
{
"%12d".writefln(1); // Writes " 1"
"%12d".writefln(0); // Writes " 0"
"%12,d".writefln(1); // Writes " 1"
"%12,d".writefln(0); // Writes "0" <-- WRONG
"%12,d".writefln(-1); // Writes " -1"
}
I've tracked the cause to the formatUnsigned function in format.d specifically the spacesToPrint calculation (ln:2351).
Here the "digits.length - 1" calculation overflows/underflows resulting in no left padding to occur.