1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-22 13:33:52 +03:00

Feature #3264: Move float_to_str to NebulaUtil

This commit is contained in:
Carlos Martín 2014-11-06 17:41:20 +01:00
parent f990a66e09
commit 8198887ca2
4 changed files with 35 additions and 34 deletions

View File

@ -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_ */

View File

@ -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

View File

@ -27,6 +27,7 @@
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <math.h>
using namespace std;
@ -238,3 +239,24 @@ vector<string> 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();
}

View File

@ -15,7 +15,7 @@
/* -------------------------------------------------------------------------- */
#include "Quota.h"
#include <math.h>
#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();
}