On 2017-11-10 06:38, Alain Ketterlin wrote:
> Lynn McGuire <
lynnmc...@gmail.com> writes:
>
>> On 11/9/2017 8:07 PM, Lynn McGuire wrote:
>>> Is there a standard way to convert a string containing a comma to a
>>> number ? atof and strtod do not work with the comma.
>>>
>>> Thanks,
>>> Lynn
>>
>> A string such as 4,800.1 or 3,334.5e9.
>
> Any of atof, strtod, sscanf will happily convert this, *provided* the
> locale is setup correctly. Check setlocale (std::setlocale), especially
> the LC_NUMERIC category.
The C standard provides an official definition of the "decimal-point
character", explicitly identifying it as locale-specific, and frequently
uses that term when describing the behavior of the functions which
convert between floating point numbers and their representations as
strings. In particular, strtod() recognizes
"a nonempty sequence of decimal digits optionally containing a
decimal-point characters, then an optional exponent..." (7.22.1.3p3).
However, none of the other members of struct lconv are mentioned
anywhere in the standard other than the part describing <locale.h>. For
none of those members is there any function in the C standard library
whose defined behavior requires it to pay attention to the value of that
member in the current locale. The decimal-point character is the only
member of that struct for which there's a corresponding item listed in
section J.4, which summarizes locale-specific behavior. It is apparently
up to the user to write code whose behavior depends upon those values.
Note, in particular, that the description of strtod() above makes no
mention of the thousands_sep or grouping members of struct lconv. The
behavior of atof() and sscanf() are both defined in terms of calls to
strtod(), so the same is true of them.
> See also
>
https://stackoverflow.com/questions/28470656/convert-string-with-thousands-and-decimal-separator-into-double
C++ differs from C in that there are C++ standard library routines that
do have behavior that depend upon values of thousands_sep and grouping
in the currently imbued locale, and that link shows how to take
advantage of that fact. Note, in particular, that even in C++,
std::strtod(), std::atof(), and std::sscanf() are NOT examples of this.
Their behavior is the same as the C standard library versions of those
routines in this regard.