aboutsummaryrefslogtreecommitdiff
path: root/src/utils/utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/utils.cpp')
-rw-r--r--src/utils/utils.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp
new file mode 100644
index 0000000..a1818a0
--- /dev/null
+++ b/src/utils/utils.cpp
@@ -0,0 +1,23 @@
+#include "utils/utils.hpp"
+
+#include <cmath>
+#include <cstdio>
+
+void format_rate(std::string & s, double rate) {
+ static const char MAX_FORMAT[] = "00000.00";
+
+ if(std::isinf(rate) || std::isnan(rate)) {
+ rate = 0.0;
+ }
+
+ s.resize(sizeof(MAX_FORMAT));
+
+ int wrlen = snprintf(&s[0], s.size(), "%05.2f", rate);
+ int sz = strlen(s.c_str());
+
+ if(sz != wrlen) {
+ s.assign("ERR");
+ } else {
+ s.resize(sz);
+ }
+}