bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Léa Gris
Subject: Re: built-in printf %f parameter format depend on LC_NUMERIC
Date: Tue, 9 Jul 2019 21:39:41 +0200
User-agent: Telnet/1.0 [tlh] (PDP11/DEC)

Le 09/07/2019 à 20:28, Chet Ramey écrivait :
On 7/9/19 11:26 AM, address@hidden 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

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]