From 43687b4781cdcd7c4e299dc625263bc3982a529f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 17 Oct 2018 15:52:11 +0200 Subject: [core] add ability to stringify doubles that are integer with and without trailing ".0" --- include/mbgl/util/string.hpp | 6 +++--- src/mbgl/util/dtoa.cpp | 4 ++-- src/mbgl/util/dtoa.hpp | 2 +- src/mbgl/util/string.cpp | 13 +++++++------ test/util/string.test.cpp | 5 +++++ 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/include/mbgl/util/string.hpp b/include/mbgl/util/string.hpp index 13498ccb92..1cca386e21 100644 --- a/include/mbgl/util/string.hpp +++ b/include/mbgl/util/string.hpp @@ -49,9 +49,9 @@ inline std::string toString(uint8_t num) { return std::to_string(unsigned(num)); } -std::string toString(float); -std::string toString(double); -std::string toString(long double); +std::string toString(float, bool decimal = false); +std::string toString(double, bool decimal = false); +std::string toString(long double, bool decimal = false); inline std::string toString(std::exception_ptr error) { assert(error); diff --git a/src/mbgl/util/dtoa.cpp b/src/mbgl/util/dtoa.cpp index fce1f122b7..934a78f7ab 100644 --- a/src/mbgl/util/dtoa.cpp +++ b/src/mbgl/util/dtoa.cpp @@ -13,12 +13,12 @@ namespace util { #if !defined(_WINDOWS) -std::string dtoa(double value) { +std::string dtoa(double value, bool decimal) { std::string data; data.resize(25); auto end = rapidjson::internal::dtoa(value, const_cast(data.data())); auto length = end - data.data(); - if (length >= 3 && end[-1] == '0' && end[-2] == '.') { + if (!decimal && length >= 3 && end[-1] == '0' && end[-2] == '.') { // Remove trailing ".0" for integers length -= 2; } diff --git a/src/mbgl/util/dtoa.hpp b/src/mbgl/util/dtoa.hpp index e6b1659aa2..22aa925675 100644 --- a/src/mbgl/util/dtoa.hpp +++ b/src/mbgl/util/dtoa.hpp @@ -6,7 +6,7 @@ namespace mbgl { namespace util { -std::string dtoa(double value); +std::string dtoa(double value, bool decimal = false); inline double stod(const std::string& str) { return ::strtod(str.c_str(), nullptr); diff --git a/src/mbgl/util/string.cpp b/src/mbgl/util/string.cpp index 4738209382..b20b3fb7ed 100644 --- a/src/mbgl/util/string.cpp +++ b/src/mbgl/util/string.cpp @@ -4,16 +4,17 @@ namespace mbgl { namespace util { -std::string toString(float num) { - return dtoa(num); +std::string toString(float num, bool decimal) { + return dtoa(num, decimal); } -std::string toString(double num) { - return dtoa(num); +std::string toString(double num, bool decimal) { + return dtoa(num, decimal); } -std::string toString(long double num) { - return dtoa(num); +std::string toString(long double num, bool decimal) { + return dtoa(num, decimal); +} } } // namespace util diff --git a/test/util/string.test.cpp b/test/util/string.test.cpp index 06e6ca9e9e..08e8e9f759 100644 --- a/test/util/string.test.cpp +++ b/test/util/string.test.cpp @@ -6,8 +6,13 @@ using namespace mbgl; TEST(ToString, FloatingPoint) { EXPECT_EQ("0", util::toString(0.0)); + EXPECT_EQ("0.0", util::toString(0.0, true)); EXPECT_EQ("1.33", util::toString(1.33)); + EXPECT_EQ("1.33", util::toString(1.33, true)); EXPECT_EQ("-1", util::toString(-1.0)); + EXPECT_EQ("-1.0", util::toString(-1.0, true)); EXPECT_EQ("12340000000", util::toString(12340000000.0)); + EXPECT_EQ("12340000000.0", util::toString(12340000000.0, true)); EXPECT_EQ("12340000000", util::toString(12340000000.0)); + EXPECT_EQ("12340000000.0", util::toString(12340000000.0, true)); } -- cgit v1.2.1