diff options
author | flu0r1ne <flu0r1ne@flu0r1ne.net> | 2023-07-13 12:20:29 -0500 |
---|---|---|
committer | flu0r1ne <flu0r1ne@flu0r1ne.net> | 2023-07-13 12:20:29 -0500 |
commit | 2cb9b6fdc38a5df00ade737f90a9daf685ab89c3 (patch) | |
tree | 0a01a5fff4785520ae1a8d400b36f6bcdf84c532 /src/utils | |
download | freqtimer-web-2cb9b6fdc38a5df00ade737f90a9daf685ab89c3.tar.xz freqtimer-web-2cb9b6fdc38a5df00ade737f90a9daf685ab89c3.zip |
Init commit
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); |