libc/stdio: fix rounding errors for fractional values less than 1

Signed-off-by: wangxuedong <wangxuedong@xiaomi.com>
This commit is contained in:
wangxuedong
2023-01-30 19:34:08 +08:00
committed by Xiang Xiao
parent fa63da22ae
commit ba016eb5eb
+3 -2
View File
@@ -127,12 +127,13 @@ int __dtoa_engine(double x, FAR struct dtoa_s *dtoa, int max_digits,
/* If limiting decimals, then limit the max digits to no more than the /* If limiting decimals, then limit the max digits to no more than the
* number of digits left of the decimal plus the number of digits right * number of digits left of the decimal plus the number of digits right
* of the decimal * of the decimal. If the integer value is 0, there are only values to
* the right of the decimal point in dtoa->digits.
*/ */
if (max_decimals != 0) if (max_decimals != 0)
{ {
max_digits = MIN(max_digits, max_decimals + MAX(exp + 1, 1)); max_digits = MIN(max_digits, max_decimals + MAX(exp + 1, 0));
} }
/* Round nearest by adding 1/2 of the last digit before converting to /* Round nearest by adding 1/2 of the last digit before converting to