added function nice_unit_format for double input (calls other after proper round...
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 1 Jun 2016 08:00:27 +0000 (10:00 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 1 Jun 2016 08:00:27 +0000 (10:00 +0200)
src/stringfunc.cpp
src/stringfunc.hxx

index 056a9cc..fa5729c 100644 (file)
@@ -27,12 +27,15 @@ on this file might be covered by the GNU General Public License.
 #include <sstream>
 #include <stdexcept>
 #include <algorithm>
+#include <cmath>    // for round()
 
 #include <wchar.h>
 #include <stdlib.h>
 #include <iconv.h>
 #include <i18n.h>
 
+#include <boost/numeric/conversion/cast.hpp>
+
 #include <stringfunc.hxx>
 
 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<int64_t>( 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);
index 4b33b6a..407b726 100644 (file)
@@ -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);