Bug 20069 – std.format digit grouping separator (aka thousands separator) needs to be revisited

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2019-07-21T16:33:00Z
Last change time
2019-08-04T12:42:56Z
Keywords
pull
Assigned to
No Owner
Creator
asumface

Comments

Comment #0 by asumface — 2019-07-21T16:33:00Z
Currently the default separator used by std.format is always ','. With the C locale (default) the number 123456.789 is thus formatted as "123,456.789,000". If another locale is activated, the output does not match the locale and sometimes even becomes a mess: writefln("%,f", 123456.789) de_DE.UTF-8: "1,234,56,,789,000" fr_FR.UTF-8: "1,234,56,,789,000" fr_CH.UTF-8: "123,456.789,000" en_IN.UTF-8: "123,456.789,000" nl_NL.UTF-8: "1,234,56,,789,000" hak_TW: "123,456.789,000" ps_AF: "12,345,6٫,789,000" unm_US: "123,456.789,000" The printf as defined by POSIX formats the same case as follows (I'd regard this as the generally correct formatting): printf("%'f", 123456.789) C: "123456.789000" de_DE.UTF-8: "123.456,789000" fr_FR.UTF-8: "123 456,789000" fr_CH.UTF-8: "123'456.789000" en_IN.UTF-8: "1,23,456.789000" (non-uniform group size!) nl_NL.UTF-8: "123456,789000" hak_TW: "12,3456.789000" ps_AF: "123٬456٫789000" unm_US: "12 34 56.789000" Grouping after the decimal separator should probably be made optional as it kind of is nonstandard behavior (scanf doesn't appear to handle such grouping, for example) yet useful for human interfacing nonetheless. Grouping should, unless overridden, behave according to the active locale, for the C locale, which is active by default, this means no grouping at all. This is useful because a formatted number such as 100000 is otherwise ambiguous for comma-decimal-users by default ("100,000").
Comment #1 by shove — 2019-07-22T03:01:36Z
(In reply to asumface from comment #0) > Currently the default separator used by std.format is always ','. > With the C locale (default) the number 123456.789 is thus formatted as > "123,456.789,000". > If another locale is activated, the output does not match the locale and > sometimes even becomes a mess: > writefln("%,f", 123456.789) > de_DE.UTF-8: "1,234,56,,789,000" > fr_FR.UTF-8: "1,234,56,,789,000" > fr_CH.UTF-8: "123,456.789,000" > en_IN.UTF-8: "123,456.789,000" > nl_NL.UTF-8: "1,234,56,,789,000" > hak_TW: "123,456.789,000" > ps_AF: "12,345,6٫,789,000" > unm_US: "123,456.789,000" > > The printf as defined by POSIX formats the same case as follows (I'd regard > this as the generally correct formatting): > printf("%'f", 123456.789) > C: "123456.789000" > de_DE.UTF-8: "123.456,789000" > fr_FR.UTF-8: "123 456,789000" > fr_CH.UTF-8: "123'456.789000" > en_IN.UTF-8: "1,23,456.789000" (non-uniform group size!) > nl_NL.UTF-8: "123456,789000" > hak_TW: "12,3456.789000" > ps_AF: "123٬456٫789000" > unm_US: "12 34 56.789000" > > Grouping after the decimal separator should probably be made optional as it > kind of is nonstandard behavior (scanf doesn't appear to handle such > grouping, for example) yet useful for human interfacing nonetheless. > Grouping should, unless overridden, behave according to the active locale, > for the C locale, which is active by default, this means no grouping at all. > This is useful because a formatted number such as 100000 is otherwise > ambiguous for comma-decimal-users by default ("100,000"). In dealing with issue 20064, I also noticed the problems. I'll handle it together.
Comment #2 by dlang-bot — 2019-07-25T03:53:52Z
@shove70 updated dlang/phobos pull request #7112 "Fix issue 20064 - format separator fails with leading zeros" fixing this issue: - Fix issue 20069 - std.format digit grouping separator (aka thousands separator) needs to be revisited https://github.com/dlang/phobos/pull/7112
Comment #3 by dlang-bot — 2019-08-04T12:42:56Z
dlang/phobos pull request #7112 "Fix issue 20064,20069 - format functions make their final width and digits grouping conform to POSIX definition" was merged into master: - 75f65a37fefd4547c3b25244d7d3ffb7ba4413cf by shove70: Fix issue 20069 - std.format digit grouping separator (aka thousands separator) needs to be revisited https://github.com/dlang/phobos/pull/7112