diff --git a/include/NebulaUtil.h b/include/NebulaUtil.h index 6aca1ecfba..db851d0e60 100644 --- a/include/NebulaUtil.h +++ b/include/NebulaUtil.h @@ -84,6 +84,15 @@ namespace one_util const std::string& st, char delim, bool clean_empty=true); + + /** + * Creates a string from the given float, using fixed notation. If the + * number has any decimals, they will be truncated to 2. + * + * @param num + * @return + */ + std::string float_to_str(const float &num); }; #endif /* _NEBULA_UTIL_H_ */ diff --git a/include/Quota.h b/include/Quota.h index 9f83bdeda6..4707db5206 100644 --- a/include/Quota.h +++ b/include/Quota.h @@ -207,15 +207,6 @@ protected: */ void cleanup_quota(const string& qid); - /** - * Creates a string from the given float, using fixed notation. If the - * number has any decimals, they will be truncated to 2. - * - * @param num - * @return - */ - string float_to_str(const float &num); - private: /** * Creates an empty quota based on the given attribute. The attribute va diff --git a/src/common/NebulaUtil.cc b/src/common/NebulaUtil.cc index 78dc8e301f..7c47e2c5b9 100644 --- a/src/common/NebulaUtil.cc +++ b/src/common/NebulaUtil.cc @@ -27,6 +27,7 @@ #include #include #include +#include using namespace std; @@ -238,3 +239,24 @@ vector one_util::split(const string& st, char delim, bool clean_empty) return parts; } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +string one_util::float_to_str(const float &num) +{ + ostringstream oss; + + if ( num == ceil(num) ) + { + oss.precision(0); + } + else + { + oss.precision(2); + } + + oss << fixed << num; + + return oss.str(); +} diff --git a/src/um/Quota.cc b/src/um/Quota.cc index 9dbac0b2fc..397e04058c 100644 --- a/src/um/Quota.cc +++ b/src/um/Quota.cc @@ -15,7 +15,7 @@ /* -------------------------------------------------------------------------- */ #include "Quota.h" -#include +#include "NebulaUtil.h" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -79,7 +79,7 @@ void Quota::add_to_quota(VectorAttribute * attr, const string& va_name, float nu total += num; - attr->replace(va_name, float_to_str(total)); + attr->replace(va_name, one_util::float_to_str(total)); } /* -------------------------------------------------------------------------- */ @@ -395,7 +395,7 @@ int Quota::update_limits( return -1; } - quota->replace(metrics[i], float_to_str(limit_f)); + quota->replace(metrics[i], one_util::float_to_str(limit_f)); } return 0; @@ -431,7 +431,7 @@ VectorAttribute * Quota::new_quota(VectorAttribute * va) return 0; } - limits.insert(make_pair(metrics[i], float_to_str(limit_f))); + limits.insert(make_pair(metrics[i], one_util::float_to_str(limit_f))); limits.insert(make_pair(metrics_used, "0")); } @@ -444,24 +444,3 @@ VectorAttribute * Quota::new_quota(VectorAttribute * va) return new VectorAttribute(template_name,limits); } - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -string Quota::float_to_str(const float &num) -{ - ostringstream oss; - - if ( num == ceil(num) ) - { - oss.precision(0); - } - else - { - oss.precision(2); - } - - oss << fixed << num; - - return oss.str(); -}