diff --git a/apps/memcached/memcache.cc b/apps/memcached/memcache.cc index 115aa0ed4..f61af22a6 100644 --- a/apps/memcached/memcache.cc +++ b/apps/memcached/memcache.cc @@ -650,7 +650,7 @@ class cache { ss << "size: " << _cache.size() << "\n"; ss << "buckets: " << _cache.bucket_count() << "\n"; - ss << "load: " << to_sstring_sprintf((double)_cache.size() / _cache.bucket_count(), "%.2lf") << "\n"; + ss << "load: " << sprint("%.2lf", (double)_cache.size() / _cache.bucket_count()) << "\n"; ss << "max bucket occupancy: " << max_size << "\n"; ss << "bucket occupancy histogram:\n"; diff --git a/core/sstring.hh b/core/sstring.hh index df151bb0d..b2b21f0d7 100644 --- a/core/sstring.hh +++ b/core/sstring.hh @@ -584,7 +584,7 @@ static String make_sstring(Args&&... args) template inline string_type to_sstring_sprintf(T value, const char* fmt) { - char tmp[sizeof(value) * 3 + 3]; + char tmp[sizeof(value) * 3 + 2]; auto len = std::sprintf(tmp, fmt, value); using char_type = typename string_type::value_type; return string_type(reinterpret_cast(tmp), len); @@ -636,19 +636,19 @@ string_type to_sstring(unsigned long long value) { template inline string_type to_sstring(float value) { - return to_sstring_sprintf(value, "%f"); + return to_sstring_sprintf(value, "%g"); } template inline string_type to_sstring(double value) { - return to_sstring_sprintf(value, "%f"); + return to_sstring_sprintf(value, "%g"); } template inline string_type to_sstring(long double value) { - return to_sstring_sprintf(value, "%Lf"); + return to_sstring_sprintf(value, "%Lg"); } template diff --git a/tests/httpd.cc b/tests/httpd.cc index a7e4d1f15..911361983 100644 --- a/tests/httpd.cc +++ b/tests/httpd.cc @@ -78,7 +78,7 @@ SEASTAR_TEST_CASE(test_formatter) sstring str = "abc"; BOOST_REQUIRE_EQUAL(json::formatter::to_json(str), "\"abc\""); float f = 1; - BOOST_REQUIRE_EQUAL(json::formatter::to_json(f), "1.000000"); + BOOST_REQUIRE_EQUAL(json::formatter::to_json(f), "1"); f = 1.0/0.0; BOOST_CHECK_THROW(json::formatter::to_json(f), std::out_of_range); f = -1.0/0.0; @@ -86,7 +86,7 @@ SEASTAR_TEST_CASE(test_formatter) f = 0.0/0.0; BOOST_CHECK_THROW(json::formatter::to_json(f), std::invalid_argument); double d = -1; - BOOST_REQUIRE_EQUAL(json::formatter::to_json(d), "-1.000000"); + BOOST_REQUIRE_EQUAL(json::formatter::to_json(d), "-1"); d = 1.0/0.0; BOOST_CHECK_THROW(json::formatter::to_json(d), std::out_of_range); d = -1.0/0.0;