diff --git a/src/builtin_math.cpp b/src/builtin_math.cpp index 41622d602..52ef83bae 100644 --- a/src/builtin_math.cpp +++ b/src/builtin_math.cpp @@ -141,6 +141,13 @@ static wcstring math_describe_error(te_error_t& error) { /// Return a formatted version of the value \p v respecting the given \p opts. static wcstring format_double(double v, const math_cmd_opts_t &opts) { + // As a special-case, a scale of 0 means to truncate to an integer + // instead of rounding. + if (opts.scale == 0) { + v = std::trunc(v); + return format_string(L"%.*f", opts.scale, v); + } + wcstring ret = format_string(L"%.*f", opts.scale, v); // If we contain a decimal separator, trim trailing zeros after it, and then the separator // itself if there's nothing after it. Detect a decimal separator as a non-digit. diff --git a/tests/math.out b/tests/math.out index e1913225e..4cafd7a30 100644 --- a/tests/math.out +++ b/tests/math.out @@ -3,7 +3,7 @@ # Validate basic expressions 1.5 1.666667 -2 +1 1 1.667 4 @@ -27,7 +27,7 @@ 100000000000000 -1000000000000000 0 -2 +1 500000000000000 ####################