From 039461e218b76cd2a4a4f23dad0b0da58207c53b Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 16 Jun 2016 15:32:29 +0200 Subject: [PATCH] Fix unit tests for Convert::ToString refs #11483 --- lib/base/convert.cpp | 6 ++++++ lib/base/value-operators.cpp | 9 +-------- test/base-convert.cpp | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/base/convert.cpp b/lib/base/convert.cpp index 45f6a965b..5454a2fc0 100644 --- a/lib/base/convert.cpp +++ b/lib/base/convert.cpp @@ -35,6 +35,12 @@ String Convert::ToString(const Value& val) String Convert::ToString(double val) { + double integral; + double fractional = std::modf(val, &integral); + + if (fractional == 0) + return Convert::ToString(static_cast(val)); + std::ostringstream msgbuf; msgbuf << std::fixed << val; return msgbuf.str(); diff --git a/lib/base/value-operators.cpp b/lib/base/value-operators.cpp index a658180a6..580fafc73 100644 --- a/lib/base/value-operators.cpp +++ b/lib/base/value-operators.cpp @@ -56,19 +56,12 @@ Value::operator double(void) const Value::operator String(void) const { Object *object; - double dvalue, integral, fractional; switch (GetType()) { case ValueEmpty: return String(); case ValueNumber: - dvalue = boost::get(m_Value); - fractional = std::modf(dvalue, &integral); - - if (fractional != 0) - return Convert::ToString(dvalue); - else - return Convert::ToString(static_cast(dvalue)); + return Convert::ToString(boost::get(m_Value)); case ValueBoolean: if (boost::get(m_Value)) return "true"; diff --git a/test/base-convert.cpp b/test/base-convert.cpp index 57da580ef..16e84da86 100644 --- a/test/base-convert.cpp +++ b/test/base-convert.cpp @@ -46,14 +46,14 @@ BOOST_AUTO_TEST_CASE(todouble) BOOST_AUTO_TEST_CASE(tostring) { BOOST_CHECK(Convert::ToString(7) == "7"); - BOOST_CHECK(Convert::ToString(7.5) == "7.5"); + BOOST_CHECK(Convert::ToString(7.5) == "7.500000"); BOOST_CHECK(Convert::ToString("hello") == "hello"); String str = "hello"; BOOST_CHECK(Convert::ToString(str) == "hello"); BOOST_CHECK(Convert::ToString(Value(7)) == "7"); - BOOST_CHECK(Convert::ToString(Value(7.5)) == "7.5"); + BOOST_CHECK(Convert::ToString(Value(7.5)) == "7.500000"); BOOST_CHECK(Convert::ToString(Value("hello")) == "hello"); BOOST_CHECK(Convert::ToString(Value("hello hello")) == "hello hello"); }