www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20535] New: std.format: %b, %o, %x and %X on negative

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

          Issue ID: 20535
           Summary: std.format: %b, %o, %x and %X on negative integers
                    gives wrong result
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: bugzilla d-ecke.de

Currently format casts the value to it's unsigned version and prints that. This
is inconsistent with %d. Additionally, if this behaviour is wished, it can
easily be accomplished by the user casting himself before formatting.

void main()
{
    import std.format : format;

    assert(format("%b",-17) == "-10001"); // fails
    assert(format("%o",-17) == "-21"); // fails
    assert(format("%x",-17) == "-11"); // fails
    assert(format("%X",-17) == "-11"); // fails
}

--
Jan 26 2020