From: Christian Herdtweck Date: Wed, 1 Jun 2016 08:00:27 +0000 (+0200) Subject: added function nice_unit_format for double input (calls other after proper round... X-Git-Tag: v2.9~23 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=5cd64148dfb5da12bba5fa6c5b126f1a61ee8a15;p=libi2ncommon added function nice_unit_format for double input (calls other after proper round&cast) --- diff --git a/src/stringfunc.cpp b/src/stringfunc.cpp index 056a9cc..fa5729c 100644 --- a/src/stringfunc.cpp +++ b/src/stringfunc.cpp @@ -27,12 +27,15 @@ on this file might be covered by the GNU General Public License. #include #include #include +#include // for round() #include #include #include #include +#include + #include using namespace std; @@ -1050,6 +1053,22 @@ string nice_unit_format( } // eo nice_unit_format(int input) +string nice_unit_format( + const double input, + const UnitFormat format, + const UnitBase base +) +{ + // round as double and cast to int64_t + // cast raised overflow error near max val of int64_t (~9.2e18, see unittest) + int64_t input_casted_and_rounded = + boost::numeric_cast( round(input) ); + + // now call other + return nice_unit_format( input_casted_and_rounded, format, base ); +} // eo nice_unit_format(double input) + + string escape(const string &s) { string out(s); diff --git a/src/stringfunc.hxx b/src/stringfunc.hxx index 4b33b6a..407b726 100644 --- a/src/stringfunc.hxx +++ b/src/stringfunc.hxx @@ -227,6 +227,12 @@ std::string nice_unit_format( const UnitBase base = UnitBase1024 ); +std::string nice_unit_format( + const double input, + const UnitFormat format = ShortUnitFormat, + const UnitBase base = UnitBase1024 +); + bool replace_all(std::string &base, const std::string *ist, const std::string *soll); bool replace_all(std::string &base, const char *ist, const char *soll); bool replace_all(std::string &base, const char *ist, const std::string *soll);