Math: Truncate integers (scale == 0)

Fixes #5251.
This commit is contained in:
Fabian Homborg 2018-10-21 12:20:18 +02:00
parent c5d72332ba
commit afc82ff23e
2 changed files with 9 additions and 2 deletions

View file

@ -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. /// 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) { 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); wcstring ret = format_string(L"%.*f", opts.scale, v);
// If we contain a decimal separator, trim trailing zeros after it, and then the separator // 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. // itself if there's nothing after it. Detect a decimal separator as a non-digit.

View file

@ -3,7 +3,7 @@
# Validate basic expressions # Validate basic expressions
1.5 1.5
1.666667 1.666667
2 1
1 1
1.667 1.667
4 4
@ -27,7 +27,7 @@
100000000000000 100000000000000
-1000000000000000 -1000000000000000
0 0
2 1
500000000000000 500000000000000
#################### ####################