diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/utils.cpp | 23 | ||||
-rw-r--r-- | src/utils/utils.hpp | 5 |
2 files changed, 28 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); + } +} diff --git a/src/utils/utils.hpp b/src/utils/utils.hpp new file mode 100644 index 0000000..7c5a546 --- /dev/null +++ b/src/utils/utils.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include <string> + +void format_rate(std::string & s, double rate); |