From ba016eb5eb727336237f08b80f7c7527807a4ae2 Mon Sep 17 00:00:00 2001 From: wangxuedong Date: Mon, 30 Jan 2023 19:34:08 +0800 Subject: [PATCH] libc/stdio: fix rounding errors for fractional values less than 1 Signed-off-by: wangxuedong --- libs/libc/stdio/lib_dtoa_engine.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libs/libc/stdio/lib_dtoa_engine.c b/libs/libc/stdio/lib_dtoa_engine.c index f9abd97936d..f508bb2a2d1 100644 --- a/libs/libc/stdio/lib_dtoa_engine.c +++ b/libs/libc/stdio/lib_dtoa_engine.c @@ -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 * 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) { - 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