www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is there a way to output formatted text following the locale settings

reply someone <someone somewhere.com> writes:
The following works as expected but the format string will end 
hard-coded:

import std.stdio;
import std.format;

void main (

    ) {

    int intAmount = 1234567;

    writeln(format("%,d", intAmount)); /// eg: 1,234,567

}

I tried something like:

import std.stdio;
import std.format;
import core.stdc.locale;

void main (

    ) {

    setlocale(LC_ALL, "");

    int intAmount = 1234567;

    writeln(format("%`d", intAmount)); /// please, note the 
backtick instead of ,

}

... to no avail :(
May 25
parent reply Paul Backus <snarwin gmail.com> writes:
On Tuesday, 25 May 2021 at 22:37:25 UTC, someone wrote:
    writeln(format("%`d", intAmount)); /// please, note the 
 backtick instead of ,
From [the documentation of `formattedWrite`][1]:
 Separator

    Inserts the separator symbols ',' every X digits, from right 
 to left, into numeric values to increase readability. The 
 fractional part of floating point values inserts the separator 
 from left to right. Entering an integer after the ',' allows to 
 specify X. If a '*' is placed after the ',' then X is specified 
 by an additional parameter to the format function. Adding a '?' 
 after the ',' or X specifier allows to specify the separator 
 character as an additional parameter.
So the syntax you want is: writeln(format("%,?d", '`', intAmount)); [1]: https://forum.dlang.org/post/vedxtnkolsmdlyfvmhsr forum.dlang.org
May 25
parent reply someone <someone somewhere.com> writes:
On Tuesday, 25 May 2021 at 23:41:39 UTC, Paul Backus wrote:

 From [the documentation of `formattedWrite`][1]:

 Separator

    Inserts the separator symbols ',' every X digits, from 
 right to left, into numeric values to increase readability. 
 The fractional part of floating point values inserts the 
 separator from left to right. Entering an integer after the 
 ',' allows to specify X. If a '*' is placed after the ',' then 
 X is specified by an additional parameter to the format 
 function. Adding a '?' after the ',' or X specifier allows to 
 specify the separator character as an additional parameter.
So the syntax you want is: writeln(format("%,?d", '`', intAmount));
Thanks for your reply, but no, let me clarify: I do not want 123\`456\`789 or whatever else custom delimiter. I just want to output what is already set in the LC_ALL locale environment variable which in my case results in 123,456,789 which of course I can mimic hard-coding it, but the idea is ... quite the opposite. The ` (backtick) I used in my example was from another example I found somewhere on the net saying that, when you use the backtick character after issuing a setlocale() call, you'll get the output accordingly to the LC_ALL variable. Needless to say I am not sure whether this is correct or not because the documentation I came across is confusing to say the least; furthermore, someone saying not every implementation is compatible with this behavior and someone else claiming it works for POSIX or the reverse I do not remember correctly right now. I do not need to use std.format.format(). I can welcome any other function in the standard library to achieve this.
May 25
parent reply Paul Backus <snarwin gmail.com> writes:
On Wednesday, 26 May 2021 at 00:18:29 UTC, someone wrote:
 Thanks for your reply, but no, let me clarify:

 I do not want 123\`456\`789 or whatever else custom delimiter.

 I just want to output what is already set in the LC_ALL locale 
 environment variable which in my case results in 123,456,789 
 which of course I can mimic hard-coding it, but the idea is ... 
 quite the opposite.
In that case, you will want to use the C library functions from [`core.stdc.stdio`][1]. [1]: https://dlang.org/phobos/core_stdc_stdio.html
May 25
parent reply someone <someone somewhere.com> writes:
On Wednesday, 26 May 2021 at 00:40:30 UTC, Paul Backus wrote:
 In that case, you will want to use the C library functions from 
 [`core.stdc.stdio`][1].
```d import core.stdc.stdio; import core.stdc.locale; import std.stdio; void main ( ) { core.stdc.locale.setlocale(LC_ALL, ""); int intAmount = 1234567; core.stdc.stdio.printf("%'d", intAmount); /// Deprecation: format specifier `"%'"` is invalid ... but it works: 1,234,567 as specified in my locale :) } ``` But I can't use writeln() formatted with the ' character to get on what's defined on my locale. PS: Previously I stated the backtick, I was wrong, the format specifier to get what's on the locale is the single quote, sorry for that.
May 28
parent reply Paul Backus <snarwin gmail.com> writes:
On Saturday, 29 May 2021 at 02:26:57 UTC, someone wrote:
 ```d
    core.stdc.stdio.printf("%'d", intAmount); /// Deprecation: 
 format specifier `"%'"` is invalid ... but it works: 1,234,567 
 as specified in my locale :)
 ```
D warns for format specifiers that do not conform to the C99 standard, and the `'` flag character is not part of C99. GCC gives a similar warning with `-Wall -pedantic`: ``` $ gcc -Wall -pedantic -o example example.c example.c: In function ‘main’: example.c:5:9: warning: ISO C does not support the ''' printf flag [-Wformat=] printf("%'d\n", 123456789); ^~~~~~~ ```
May 28
parent someone <someone somewhere.com> writes:
On Saturday, 29 May 2021 at 02:59:43 UTC, Paul Backus wrote:

 D warns for format specifiers that do not conform to the C99 
 standard, and the `'` flag character is not part of C99. GCC 
 gives a similar warning with `-Wall -pedantic`:
Crystal clear !
May 28