diff --git a/src/builtin_math.cpp b/src/builtin_math.cpp index 8ecb3ef89..ab7dadf36 100644 --- a/src/builtin_math.cpp +++ b/src/builtin_math.cpp @@ -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(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; }