[math] Set LC_NUMERIC to C

This allows us to always use "." as radix character, so e.g.

    math 2.5 - 2

is always valid, regardless of locale.
This commit is contained in:
Fabian Homborg 2018-02-11 15:44:35 +01:00
parent 234ebb5d7b
commit ce28891c76

View file

@ -120,7 +120,14 @@ static int evaluate_expression(const wchar_t *cmd, parser_t &parser, io_streams_
int error;
char *narrow_str = wcs2str(expression);
// Switch locale while computing stuff.
// This means that the "." is always the radix character,
// so numbers work the same across locales.
char *saved_locale = strdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C");
double v = te_interp(narrow_str, &error);
setlocale(LC_NUMERIC, saved_locale);
if (error == 0) {
if (opts.scale == 0) {
streams.out.append_format(L"%ld\n", static_cast<long>(v));
@ -132,6 +139,7 @@ static int evaluate_expression(const wchar_t *cmd, parser_t &parser, io_streams_
streams.err.append_format(L"'%ls': Error at token %d\n", expression.c_str(), error - 1);
}
free(narrow_str);
free(saved_locale);
return STATUS_CMD_OK;
}