D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6925 - Obey locale for thousands grouping syntax in writef?
Summary: Obey locale for thousands grouping syntax in writef?
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: bootcamp
Depends on:
Blocks:
 
Reported: 2011-11-09 20:11 UTC by bearophile_hugs
Modified: 2021-04-11 08:38 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-11-09 20:11:23 UTC
Quotation from: http://linux.die.net/man/3/printf

> For some numeric conversions a radix character ("decimal point") or thousands'
> grouping character is used. The actual character used depends on the LC_NUMERIC
> part of the locale. The POSIX locale uses '.' as radix character, and does not
> have a grouping character. Thus,
> 
>     printf("%'.2f", 1234567.89);
> 
> results in "1234567.89" in the POSIX locale, in "1234567,89" in the nl_NL
> locale, and in "1.234.567,89" in the da_DK locale.

Maybe it's good to support this syntax in writef/writefln too. But I don't know if using the locale is a good idea here.
Comment 1 Infiltrator 2014-03-19 21:36:35 UTC
Would you like to create a PR for this?
Comment 2 Berni44 2021-02-06 19:57:09 UTC
Meanwhile 

writef("%.2,f", 1234567.89);

does this grouping. Result: "1,234,567.89" It does not support a locale though. And if I should guess, it will never do, because that would make the functions non pure. A possible approach would probably be, to add a special module for localization that calls writef and modifies the result according to the locale.
Comment 3 Andrei Alexandrescu 2021-02-07 03:14:11 UTC
(In reply to Berni44 from comment #2)
> Meanwhile 
> 
> writef("%.2,f", 1234567.89);
> 
> does this grouping. Result: "1,234,567.89" It does not support a locale
> though. And if I should guess, it will never do, because that would make the
> functions non pure. A possible approach would probably be, to add a special
> module for localization that calls writef and modifies the result according
> to the locale.

A way to keep purity with locales would be to pass the locale into the functions, e.g.

writef(mylocale, "%.2,f", 1234567.89);
Comment 4 Berni44 2021-02-07 07:48:31 UTC
(In reply to Andrei Alexandrescu from comment #3)
> A way to keep purity with locales would be to pass the locale into the
> functions, e.g.
> 
> writef(mylocale, "%.2,f", 1234567.89);

Sure, but it would make the functions much more complicated - for example think of different digit shapes used in some areas of the world or even printing from right to left (what's the minus-flag supposed to do in that case?)... I'd prefer to have localization in a wrapper in a different module.
Comment 5 Berni44 2021-04-11 08:37:58 UTC
I'm closing this, because the support for grouping exists and localization is a different thing. I opened a new one for this: Issue 21819.