We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent e9375ed commit 369b7bbCopy full SHA for 369b7bb
2 files changed
printf.c
@@ -363,11 +363,7 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
363
364
if (prec == 0U) {
365
diff = value - (double)whole;
366
- if (diff > 0.5) {
367
- // greater than 0.5, round up, e.g. 1.6 -> 2
368
- ++whole;
369
- }
370
- else if ((diff == 0.5) && (whole & 1)) {
+ if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
371
// exactly 0.5 and ODD, then round up
372
// 1.5 -> 2, but 2.5 -> 2
373
++whole;
test/test_suite.cpp
@@ -1036,6 +1036,15 @@ TEST_CASE("float", "[]" ) {
1036
test::sprintf(buffer, "%.0f", 34.1415354);
1037
REQUIRE(!strcmp(buffer, "34"));
1038
1039
+ test::sprintf(buffer, "%.0f", 1.3);
1040
+ REQUIRE(!strcmp(buffer, "1"));
1041
+
1042
+ test::sprintf(buffer, "%.0f", 1.55);
1043
+ REQUIRE(!strcmp(buffer, "2"));
1044
1045
+ test::sprintf(buffer, "%.1f", 1.64);
1046
+ REQUIRE(!strcmp(buffer, "1.6"));
1047
1048
test::sprintf(buffer, "%.2f", 42.8952);
1049
REQUIRE(!strcmp(buffer, "42.90"));
1050
@@ -1082,6 +1091,9 @@ TEST_CASE("float", "[]" ) {
1082
1091
test::sprintf(buffer, "%.0f", 3.5);
1083
1092
REQUIRE(!strcmp(buffer, "4"));
1084
1093
1094
+ test::sprintf(buffer, "%.0f", 4.5);
1095
+ REQUIRE(!strcmp(buffer, "4"));
1096
1085
1097
test::sprintf(buffer, "%.0f", 3.49);
1086
1098
REQUIRE(!strcmp(buffer, "3"));
1087
1099
0 commit comments