From 9b45f323d0047c4ac6fbf56dfb80a44d8ec45d4a Mon Sep 17 00:00:00 2001 From: David Zafman Date: Thu, 8 Aug 2019 18:06:43 -0700 Subject: [PATCH] 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) --- src/common/Formatter.cc | 26 ++++++++++++++++++++++++++ src/common/Formatter.h | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/common/Formatter.cc b/src/common/Formatter.cc index 2264c71171bc0..6450859a599ac 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 4a9ac3210e083..3e9e1df9f6569 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 -- 2.39.5