diff --git a/base/base_tests/string_utils_test.cpp b/base/base_tests/string_utils_test.cpp index edb66c7986..9640253548 100644 --- a/base/base_tests/string_utils_test.cpp +++ b/base/base_tests/string_utils_test.cpp @@ -234,6 +234,15 @@ 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(-1.0E2), "-100", ()); + TEST_EQUAL(strings::to_string(1.0E-2), "0.01", ()); + + TEST_EQUAL(strings::to_string(123456789123456789ULL), "123456789123456789", ()); + TEST_EQUAL(strings::to_string(-987654321987654321LL), "-987654321987654321", ()); +} + +UNIT_TEST(to_string_dac) +{ 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", ()); @@ -243,11 +252,11 @@ UNIT_TEST(to_string) TEST_EQUAL(strings::to_string_dac(-0.333333, 4), "-0.3333", ()); TEST_EQUAL(strings::to_string_dac(2.33, 2), "2.33", ()); - TEST_EQUAL(strings::to_string(-1.0E2), "-100", ()); - TEST_EQUAL(strings::to_string(1.0E-2), "0.01", ()); + TEST_EQUAL(strings::to_string_dac(-0.0039, 2), "-0", ()); + TEST_EQUAL(strings::to_string_dac(0.0039, 2), "0", ()); - TEST_EQUAL(strings::to_string(123456789123456789ULL), "123456789123456789", ()); - TEST_EQUAL(strings::to_string(-987654321987654321LL), "-987654321987654321", ()); + TEST_EQUAL(strings::to_string_dac(0., 5), "0", ()); + TEST_EQUAL(strings::to_string_dac(0., 0), "0", ()); } struct FunctorTester diff --git a/base/string_utils.cpp b/base/string_utils.cpp index a4e363671f..6a5c42bb9a 100644 --- a/base/string_utils.cpp +++ b/base/string_utils.cpp @@ -169,6 +169,25 @@ bool StartsWith(string const & s1, char const * s2) string to_string_dac(double d, int dac) { + ostringstream ss; + if (d < 1. && d > -1.) + { + string res; + if (d >= 0.) + { + ss << setprecision(dac + 1) << d + 1; + res = ss.str(); + res[0] = '0'; + } + else + { + ss << setprecision(dac + 1) << d - 1; + res = ss.str(); + res[1] = '0'; + } + return res; + } + double fD = fabs(d); // Calc digits before comma (log10). @@ -179,7 +198,6 @@ string to_string_dac(double d, int dac) ++dbc; } - ostringstream ss; ss << setprecision(dac + dbc) << d; return ss.str(); } diff --git a/map/map_tests/measurement_tests.cpp b/map/map_tests/measurement_tests.cpp index 1eb08e2d5c..429e87650b 100644 --- a/map/map_tests/measurement_tests.cpp +++ b/map/map_tests/measurement_tests.cpp @@ -48,11 +48,11 @@ UNIT_TEST(LatLonToDMS_Rounding) { // Here and after data is from Wiki: http://bit.ly/datafotformatingtest // Boston - TEST_EQUAL(FormatLatLonAsDMS(42.358056, -71.063611), "42°21′29″N 71°03′49″W", ()); + TEST_EQUAL(FormatLatLonAsDMS(42.358056, -71.063611, 0), "42°21′29″N 71°03′49″W", ()); // Minsk - TEST_EQUAL(FormatLatLonAsDMS(53.916667, 27.55), "53°55′00″N 27°33′00″E", ()); + TEST_EQUAL(FormatLatLonAsDMS(53.916667, 27.55, 0), "53°55′00″N 27°33′00″E", ()); // Rio - TEST_EQUAL(FormatLatLonAsDMS(-22.908333, -43.196389), "22°54′30″S 43°11′47″W", ()); + TEST_EQUAL(FormatLatLonAsDMS(-22.908333, -43.196389, 0), "22°54′30″S 43°11′47″W", ()); } UNIT_TEST(LatLonToDMS_NoRounding) @@ -61,4 +61,6 @@ UNIT_TEST(LatLonToDMS_NoRounding) TEST_EQUAL(FormatLatLonAsDMS(48.8567, 2.3508, 2), "48°51′24.12″N 02°21′02.88″E", ()); // Vatican TEST_EQUAL(FormatLatLonAsDMS(41.904, 12.453, 2), "41°54′14.4″N 12°27′10.8″E", ()); + + TEST_EQUAL(FormatLatLonAsDMS(21.981112, -159.371112, 2), "21°58′52″N 159°22′16″W", ()); }