Commit 18bddb39 authored by Adrien Béraud's avatar Adrien Béraud

string utils: bring back high precision to_string

Change-Id: I2829c6b42fbebec200b3935aadc62b60498effd0
parent 2353b70d
...@@ -70,6 +70,16 @@ bstrToStdString(BSTR bstr) ...@@ -70,6 +70,16 @@ bstrToStdString(BSTR bstr)
} }
#endif #endif
std::string
to_string(double value)
{
char buf[64];
int len = snprintf(buf, sizeof(buf), "%-.*G", 16, value);
if (len <= 0)
throw std::invalid_argument{"can't parse double"};
return {buf, (size_t)len};
}
std::string std::string
trim(const std::string &s) trim(const std::string &s)
{ {
......
...@@ -39,6 +39,8 @@ bool_to_str(bool b) noexcept ...@@ -39,6 +39,8 @@ bool_to_str(bool b) noexcept
return b ? TRUE_STR : FALSE_STR; return b ? TRUE_STR : FALSE_STR;
} }
std::string to_string(double value);
#ifdef _WIN32 #ifdef _WIN32
std::wstring to_wstring(const std::string& s); std::wstring to_wstring(const std::string& s);
std::string decodeMultibyteString(const std::string& s); std::string decodeMultibyteString(const std::string& s);
......
...@@ -45,9 +45,9 @@ private: ...@@ -45,9 +45,9 @@ private:
CPPUNIT_TEST(split_string_test); CPPUNIT_TEST(split_string_test);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
const double DOUBLE = 3.141593; const double DOUBLE = 3.14159265359;
const int INT = 42 ; const int INT = 42 ;
const std::string PI_DOUBLE = "3.141593"; const std::string PI_DOUBLE = "3.14159265359";
const std::string PI_FLOAT = "3.14159265359"; const std::string PI_FLOAT = "3.14159265359";
const std::string PI_42 = "42"; const std::string PI_42 = "42";
}; };
...@@ -65,11 +65,11 @@ void ...@@ -65,11 +65,11 @@ void
StringUtilsTest::to_string_test() StringUtilsTest::to_string_test()
{ {
// test with double // test with double
CPPUNIT_ASSERT(std::to_string(DOUBLE) == PI_DOUBLE); CPPUNIT_ASSERT(to_string(DOUBLE) == PI_DOUBLE);
// test with float // test with float
float varFloat = 3.14; float varFloat = 3.14;
std::string sVarFloat = std::to_string(varFloat); std::string sVarFloat = to_string(varFloat);
CPPUNIT_ASSERT(sVarFloat.at(0) == '3' && sVarFloat.at(1) == '.' CPPUNIT_ASSERT(sVarFloat.at(0) == '3' && sVarFloat.at(1) == '.'
&& sVarFloat.at(2) == '1' && sVarFloat.at(3) == '4'); && sVarFloat.at(2) == '1' && sVarFloat.at(3) == '4');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment