Thread overview
is there a way to output formatted text following the locale settings ?
May 25, 2021
someone
May 25, 2021
Paul Backus
May 26, 2021
someone
May 26, 2021
Paul Backus
May 29, 2021
someone
May 29, 2021
Paul Backus
May 29, 2021
someone
May 25, 2021

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, 2021

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:

>

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));
May 26, 2021

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 26, 2021

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.

May 29, 2021

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].

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 29, 2021

On Saturday, 29 May 2021 at 02:26:57 UTC, someone wrote:

>
   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 29, 2021

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 !