[Issue 18838] New: Formatting the number zero with separator doesn't obey width specifier

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon May 7 04:45:38 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18838

          Issue ID: 18838
           Summary: Formatting the number zero with separator doesn't obey
                    width specifier
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: minor
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: tornchi at gmail.com

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.

--


More information about the Digitalmars-d-bugs mailing list