[Top][All Lists]

[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, lea.gris@noiraude.net wrote:

Bash Version: 5.0
Patch Level: 3
Release Status: release

         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:


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

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

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

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

locale -ck thousands_sep
thousands_sep=" "

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

Léa Gris

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

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