From: YongHao Hu Subject: msvcrt: Fix inconsistent rounding behaviour for sprintf and add tests. Message-Id: <54C624FD.4050109@gmail.com> Date: Mon, 26 Jan 2015 19:29:01 +0800 Fix bug https://bugs.winehq.org/show_bug.cgi?id=37913 . --- dlls/msvcrt/printf.h | 7 ++++++- dlls/msvcrt/tests/printf.c | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/dlls/msvcrt/printf.h b/dlls/msvcrt/printf.h index 80133d6..d0815a9 100644 --- a/dlls/msvcrt/printf.h +++ b/dlls/msvcrt/printf.h @@ -338,7 +338,7 @@ int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const API MSVCRT_pthreadlocinfo locinfo; const APICHAR *q, *p = fmt; APICHAR buf[32]; - int written = 0, pos, i; + int written = 0, pos, i, temp; FUNC_NAME(pf_flags) flags; TRACE("Format is: %s\n", FUNC_NAME(debugstr)(fmt)); @@ -584,6 +584,11 @@ int FUNC_NAME(pf_printf)(FUNC_NAME(puts_clbk) pf_puts, void *puts_ctx, const API val = -val; } + if(flags.Precision >= 0 && toupper(flags.Format)!='G') { + temp = pow(10, flags.Precision); + val = round(val * temp) / temp; + } + sprintf(tmp, float_fmt, val); if(toupper(flags.Format)=='E' || toupper(flags.Format)=='G') FUNC_NAME(pf_fixup_exponent)(tmp); diff --git a/dlls/msvcrt/tests/printf.c b/dlls/msvcrt/tests/printf.c index 0606c25..2688383 100644 --- a/dlls/msvcrt/tests/printf.c +++ b/dlls/msvcrt/tests/printf.c @@ -547,6 +547,31 @@ static void test_sprintf( void ) ok(!strcmp(buffer,"1"), "failed\n"); ok( r==1, "return count wrong\n"); + format = "%.1f"; + r = sprintf(buffer, format,-3.25); + ok(!strcmp(buffer,"-3.3"), "failed\n"); + ok( r==4, "return count wrong\n"); + + format = "%.0f"; + r = sprintf(buffer, format,0.5); + ok(!strcmp(buffer,"1"), "failed\n"); + ok( r==1, "return count wrong\n"); + + format = "%.0f"; + r = sprintf(buffer, format,1.5); + ok(!strcmp(buffer,"2"), "failed\n"); + ok( r==1, "return count wrong\n"); + + format = "%.0f"; + r = sprintf(buffer, format,2.5); + ok(!strcmp(buffer,"3"), "failed\n"); + ok( r==1, "return count wrong\n"); + + format = "%.6g"; + r = sprintf(buffer, format,1.0e-9); + ok(!strcmp(buffer,"1e-009"), "failed\n"); + ok( r==6, "return count wrong\n"); + format = "%2.4e"; r = sprintf(buffer, format,8.6); ok(!strcmp(buffer,"8.6000e+000"), "failed\n");