www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18838] New: Formatting the number zero with separator doesn't


          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 puremagic.com
          Reporter: tornchi 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.

May 06 2018