From 7886965e344b29e32da789e284658fd066ad5e4e Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 8 Mar 2016 10:03:31 +0800 Subject: [PATCH] Fix a bug in dtoa This previously affects Writer:: SetMaxDecimalPlaces() --- include/rapidjson/internal/dtoa.h | 2 +- test/unittest/dtoatest.cpp | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/internal/dtoa.h b/include/rapidjson/internal/dtoa.h index d4582845..bc454960 100644 --- a/include/rapidjson/internal/dtoa.h +++ b/include/rapidjson/internal/dtoa.h @@ -180,7 +180,7 @@ inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) { buffer[1] = '.'; for (int i = 2; i < offset; i++) buffer[i] = '0'; - if (length + offset > maxDecimalPlaces) { + if (length - kk > maxDecimalPlaces) { // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1 // Remove extra trailing zeros (at least one) after truncation. for (int i = maxDecimalPlaces + 1; i > 2; i--) diff --git a/test/unittest/dtoatest.cpp b/test/unittest/dtoatest.cpp index da020957..fe28271f 100644 --- a/test/unittest/dtoatest.cpp +++ b/test/unittest/dtoatest.cpp @@ -81,6 +81,12 @@ TEST(dtoa, maxDecimalPlaces) { TEST_DTOA(3, 2.225073858507201e-308, "0.0"); // Max subnormal positive double TEST_DTOA(3, 2.2250738585072014e-308, "0.0"); // Min normal positive double TEST_DTOA(3, 1.7976931348623157e308, "1.7976931348623157e308"); // Max double + TEST_DTOA(5, -0.14000000000000001, "-0.14"); + TEST_DTOA(4, -0.14000000000000001, "-0.14"); + TEST_DTOA(3, -0.14000000000000001, "-0.14"); + TEST_DTOA(3, -0.10000000000000001, "-0.1"); + TEST_DTOA(2, -0.10000000000000001, "-0.1"); + TEST_DTOA(1, -0.10000000000000001, "-0.1"); #undef TEST_DTOA }