Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15105

Re: built-in printf %f parameter format depend on LC_NUMERIC

From Léa Gris <lea.gris@noiraude.net>
Newsgroups gnu.bash.bug
Subject Re: built-in printf %f parameter format depend on LC_NUMERIC
Date 2019-07-09 21:39 +0200
Message-ID <mailman.712.1562701203.2688.bug-bash@gnu.org> (permalink)
References <5d24be33.1c69fb81.59c43.fe4dSMTPIN_ADDED_BROKEN@mx.google.com> <a212b38c-914d-dda6-8d22-e9039063768e@case.edu> <6468b45e-5b4a-8edf-4ab8-0838843beaaf@noiraude.net>

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

Le 09/07/2019 à 20:28, Chet Ramey écrivait :
> On 7/9/19 11:26 AM, lea.gris@noiraude.net wrote:
> 
>> Bash Version: 5.0
>> Patch Level: 3
>> Release Status: release
>>
>> Description:
>>          When formatting floating point numbers in Bash's built-in
>>          printf with %s, the argument format depends on the LC_NUMERIC
>>          environment variable.
> 
> As POSIX requires:
> 
> http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_04
> 
> The `decimal_point' member of the LC_NUMERIC category defines the radix
> character in floating point numbers, for both input and output.

> This depends on the behavior of strtold/strtod. POSIX requires strtod to
> honor the radix character as defined in LC_NUMERIC. Since LC_NUMERIC
> determines the radix character for input and output, it looks like `bc'
> is not POSIX conformant.

So many different behaviors of the printf '%f' $floatnum is a minefield.

$ locale -ck decimal_point
LC_NUMERIC
decimal_point=","

GNU coreutils 8.30 printf input POSIX/C's LC_NUMERIC format, but output 
locale setting's LC_NUMERIC format

$env printf --version
printf (GNU coreutils) 8.30

$ env printf '%f\n' 0.5
0,500000

$ env printf '%f\n' 0,5
printf: «0,5» : valeur non complètement convertie
0,000000

Whereas Bash printf both input and output to locale setting's LC_NUMERIC 
format.

Given bc is a language and thus has to stick to a constant 
representation of floating-point numbers, it has good reasons to use a 
decimal point rather than varying floating point numbers format with 
locale settings.

It appears more logical that printf that does formatting, outputs 
accordingly to the locale settings while handling its parameters with 
the POSIX or C locale decimal_point as does the GNU coreutils printf.
It also greatly ease integration.

I won't argument anymore on the merits of either behavior, although it 
would be helpful and probably save Bash scripts writers some troubles, 
if the specifics of %f with Bash's built-in printf %f format were 
documented.

I also note that while you says the Bash's built-in printf %f honour 
LC_NUMERIC decimal_point="," locale for input and output.

I note that Bash's built-in printf %f or %d does not honour LC_NUMERIC 
thousands_sep=" " when dealing with integers.

$ printf '%f\n' '1 000 000'
bash: printf: 1 000 000: nombre non valable
1,000000

locale -ck thousands_sep
LC_NUMERIC
thousands_sep=" "

$ printf '%d\n' '1 000 000'
bash: printf: 1 000 000: nombre non valable
1

-- 
Léa Gris

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: built-in printf %f parameter format depend on LC_NUMERIC Léa Gris <lea.gris@noiraude.net> - 2019-07-09 21:39 +0200

csiph-web