From: David Zafman Date: Fri, 9 Aug 2019 01:06:43 +0000 (-0700) Subject: common: Add support routines to generate strings for fixed point X-Git-Tag: v13.2.7~26^2~7 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=9b45f323d0047c4ac6fbf56dfb80a44d8ec45d4a;p=ceph.git common: Add support routines to generate strings for fixed point Signed-off-by: David Zafman (cherry picked from commit 8ac1562b4988fc3d52f92f15eb58075de0bcf27e) Conflicts: src/common/Formatter.h (trivial) --- diff --git a/src/common/Formatter.cc b/src/common/Formatter.cc index 2264c71171b..6450859a599 100644 --- a/src/common/Formatter.cc +++ b/src/common/Formatter.cc @@ -24,6 +24,32 @@ // ----------------------- namespace ceph { +std::string +fixed_u_to_string(uint64_t num, int scale) +{ + std::ostringstream t; + + t.fill('0'); + t.width(scale + 1); + t << num; + int len = t.str().size(); + return t.str().substr(0,len - scale) + "." + t.str().substr(len - scale); +} + +std::string +fixed_to_string(int64_t num, int scale) +{ + std::ostringstream t; + bool neg = num < 0; + if (neg) num = -num; + + t.fill('0'); + t.width(scale + 1); + t << num; + int len = t.str().size(); + return (neg ? "-" : "") + t.str().substr(0,len - scale) + "." + t.str().substr(len - scale); +} + /* * FormatterAttrs(const char *attr, ...) * diff --git a/src/common/Formatter.h b/src/common/Formatter.h index 4a9ac3210e0..3e9e1df9f65 100644 --- a/src/common/Formatter.h +++ b/src/common/Formatter.h @@ -254,6 +254,7 @@ namespace ceph { std::vector< std::string > m_column_name; }; - + std::string fixed_to_string(int64_t num, int scale); + std::string fixed_u_to_string(uint64_t num, int scale); } #endif