STL icon indicating copy to clipboard operation
STL copied to clipboard

<format>: The width of output is miscalculated when formatting a floating-point number in the locale-specific form

Open cpplearner opened this issue 1 year ago • 0 comments

Describe the bug

Revealed by libc++ test std/utilities/format/format.functions/locale-specific_form.pass.cpp.

When formatting a floating-point number, digit separators are not taken into account when computing the width of output, which may cause too many fill characters in the output.

For integers, digit separators are correctly handled:

https://github.com/microsoft/STL/blob/3eac329d1f614ecf138d96c22a3b02f87076bc4a/stl/inc/format#L2873-L2878

For floating-point numbers, it seems that the width is not adjusted:

https://github.com/microsoft/STL/blob/3eac329d1f614ecf138d96c22a3b02f87076bc4a/stl/inc/format#L3099-L3102

Command-line test case

D:\test>type test-format.cpp
#include <format>
#include <iostream>

int main() {
    std::cout << std::format(std::locale("en_US"), "{:@>8L}\n", 12345);
    std::cout << std::format(std::locale("en_US"), "{:@>8L}\n", 12345.0);
}

D:\test>cl /std:c++20 /utf-8 /EHs test-format.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.39.33321 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test-format.cpp
Microsoft (R) Incremental Linker Version 14.39.33321.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test-format.exe
test-format.obj

D:\test>test-format.exe
@@12,345
@@@12,345

Expected behavior

The output should be @@12,345 instead of @@@12,345.

STL version

https://github.com/microsoft/STL/commit/3eac329d1f614ecf138d96c22a3b02f87076bc4a

cpplearner avatar Jan 14 '24 15:01 cpplearner