Le 17/06/2024 à 09:17, Koichi Murase écrivait :
declare -i numvar=${localeFormatted/[!0-9]/.}
This would break with negative numbers.
I know no other radix separator than comma or dot. If there are other
radix to replace, it can be listed in a character class.
Lets say there are locales that uses , ; or :
declare -i numvar=${localeFormatted/[,;:]/.}
In my opinion, the locale-formatted strings should only appear in the
strings presented to users, such as in the output of `printf'. It is
easier and maintainable to normalize all the internal representations
to be the C floating-point literals.
I have been in IT in France since 3 decades. And I have never ever found
any piece of code that use locale-formatted literals, not even during my
CS education.
Sometimes one have to deal with data sources with numbers formatted in
funny ways, even grouping with illegal characters. Then it is a know
fact that one must deal with sourced data with zealous sanitizing.
Now on the bash shell context. The usual pitfall with floating-point
number parsing comes when some script author tries to parse output of a
command that is not meant to be parsed, that is locale formatted and
don't bother to set LC_NUMERIC=C before running this command
A common pitfall is trying to parse the output of the time builtin command.
It already breaks on locale ignorant scripts. This won't change.
--
Léa Gris