From c66a3aa140affe7f42e93d8f3a583af120892e26 Mon Sep 17 00:00:00 2001 From: vng Date: Fri, 14 Mar 2014 15:00:06 +0300 Subject: [PATCH] [search] Use fixed digits after comma (5) for coordinates conversion. --- base/base_tests/string_utils_test.cpp | 6 ++++++ base/string_utils.cpp | 10 ++++++++++ base/string_utils.hpp | 4 ++++ search/intermediate_result.cpp | 2 +- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/base/base_tests/string_utils_test.cpp b/base/base_tests/string_utils_test.cpp index a88d8e5cd2..cb82e3fa25 100644 --- a/base/base_tests/string_utils_test.cpp +++ b/base/base_tests/string_utils_test.cpp @@ -230,6 +230,12 @@ UNIT_TEST(to_string) // 6 digits after the comma with rounding - it's a default behavior TEST_EQUAL(strings::to_string(-0.66666666), "-0.666667", ()); + TEST_EQUAL(strings::to_string_dac(99.9999, 3), "100", ()); + TEST_EQUAL(strings::to_string_dac(-99.9999, 3), "-100", ()); + TEST_EQUAL(strings::to_string_dac(-10.66666666, 7), "-10.6666667", ()); + TEST_EQUAL(strings::to_string_dac(10001.66666666, 8), "10001.66666666", ()); + TEST_EQUAL(strings::to_string_dac(99999.99999999, 8), "99999.99999999", ()); + TEST_EQUAL(strings::to_string(-1.0E2), "-100", ()); TEST_EQUAL(strings::to_string(1.0E-2), "0.01", ()); diff --git a/base/string_utils.cpp b/base/string_utils.cpp index 561308eb45..091dbc6f24 100644 --- a/base/string_utils.cpp +++ b/base/string_utils.cpp @@ -3,6 +3,9 @@ #include "../std/target_os.hpp" #include "../std/iterator.hpp" +#include "../std/cmath.hpp" + +#include // setprecision #include // boost::trim @@ -160,4 +163,11 @@ bool StartsWith(string const & s1, char const * s2) return (s1.compare(0, strlen(s2), s2) == 0); } +string to_string_dac(double d, int dac) +{ + ostringstream ss; + ss << std::setprecision(dac + int(log10(fabs(d))) + 1) << d; + return ss.str(); +} + } diff --git a/base/string_utils.hpp b/base/string_utils.hpp index 0ad003c700..f24fddf17c 100644 --- a/base/string_utils.hpp +++ b/base/string_utils.hpp @@ -239,6 +239,10 @@ inline string to_string(uint64_t i) { return impl::to_string_unsigned(i); } + +/// Use this function to get string with fixed count of +/// "Digits after comma". +string to_string_dac(double d, int dac); //@} bool StartsWith(string const & s1, char const * s2); diff --git a/search/intermediate_result.cpp b/search/intermediate_result.cpp index 25461c7d98..55d06c8b4d 100644 --- a/search/intermediate_result.cpp +++ b/search/intermediate_result.cpp @@ -194,7 +194,7 @@ PreResult2::PreResult2(FeatureType const & f, PreResult1 const * p, } PreResult2::PreResult2(m2::RectD const & viewport, m2::PointD const & pos, double lat, double lon) - : m_str("(" + strings::to_string(lat) + ", " + strings::to_string(lon) + ")"), + : m_str("(" + strings::to_string_dac(lat, 5) + ", " + strings::to_string_dac(lon, 5) + ")"), m_resultType(RESULT_LATLON), m_rank(255) {