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: v15.1.0~1664^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=8ac1562b4988fc3d52f92f15eb58075de0bcf27e;p=ceph.git common: Add support routines to generate strings for fixed point Signed-off-by: David Zafman --- diff --git a/src/common/Formatter.cc b/src/common/Formatter.cc index 746a6c025fa5..ba29b62d10ac 100644 --- a/src/common/Formatter.cc +++ b/src/common/Formatter.cc @@ -25,6 +25,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 1363f1f67687..c4cdd552373b 100644 --- a/src/common/Formatter.h +++ b/src/common/Formatter.h @@ -299,5 +299,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