diff --git a/src/io/WKTWriter.cpp b/src/io/WKTWriter.cpp index 53fa14269f..7c8f04bdcf 100644 --- a/src/io/WKTWriter.cpp +++ b/src/io/WKTWriter.cpp @@ -460,7 +460,10 @@ WKTWriter::writeTrimmedNumber(double d, uint32_t precision, char* buf) // most real-world coordinates, use positional notation if ( (precision < 4) && (da < 1.0) ) { // adjust precision to avoid rounding to zero - precision = static_cast(-floor(log10(da))); + const auto higher_prec = static_cast(-floor(log10(da))); + if (higher_prec > precision) { + precision = higher_prec; + } } return geos_d2sfixed_buffered_n(d, precision, buf); } diff --git a/tests/unit/capi/GEOS_printDoubleTest.cpp b/tests/unit/capi/GEOS_printDoubleTest.cpp index 69cd615629..0bcf624275 100644 --- a/tests/unit/capi/GEOS_printDoubleTest.cpp +++ b/tests/unit/capi/GEOS_printDoubleTest.cpp @@ -69,6 +69,21 @@ void object::test<1>() TESTCASE(12, 1.2345678901234e+17, "1.234567890123e+17"), TESTCASE(13, 1.2345678901234e+17, "1.2345678901234e+17"), TESTCASE(14, 1.2345678901234e+17, "1.2345678901234e+17"), + TESTCASE(0, 0.0123456789, "0.01"), + TESTCASE(1, 0.0123456789, "0.01"), + TESTCASE(2, 0.0123456789, "0.01"), + TESTCASE(3, 0.0123456789, "0.012"), + TESTCASE(4, 0.0123456789, "0.0123"), + TESTCASE(0, 0.123456789, "0.1"), + TESTCASE(1, 0.123456789, "0.1"), + TESTCASE(2, 0.123456789, "0.12"), + TESTCASE(3, 0.123456789, "0.123"), + TESTCASE(4, 0.123456789, "0.1235"), + TESTCASE(0, 1.23456789, "1"), + TESTCASE(1, 1.23456789, "1.2"), + TESTCASE(2, 1.23456789, "1.23"), + TESTCASE(3, 1.23456789, "1.235"), + TESTCASE(4, 1.23456789, "1.2346"), }; for (const auto& testcase : testcase_l) { char buf[28];