1#include "number_format.h"
11std::string number_format::convert(
float value)
const {
14 if(decimal_integers) {
15 s = std::to_string(
static_cast<int>(std::round(value)));
18 if(!trailing_zeros && s.length() > 1)
24 if(!s.empty() && s.front() ==
'-' || s.front() ==
'+') {
30 for(
size_t i = 0; i < s.size(); ++i) {
31 if(!std::isdigit(s[i])) {
37 s = apply_grouping(s);
38 s = sign + s + suffix;
44void number_format::precision_from_range(
float first,
float last) {
45 const float delta = std::abs(last - first);
49 unsigned max_precision = 7;
50 for(
unsigned i = 1; i <= max_precision; ++i) {
51 if(delta > limit || i == max_precision) {
59std::string number_format::apply_grouping(
const std::string& value)
const {
63 std::vector<std::string> parts;
64 int i = value.length();
69 if(length + g + 1 > value.length())
70 g = std::max(
static_cast<size_t>(1), value.length() - length);
72 parts.push_back(value.substr(i, g));
74 if(length > value.length())
78 std::reverse(parts.begin(), parts.end());
Helper functions to convert numeric types into strings using std streams.
std::string join(const InputIt first, const InputIt last, const std::string &separator, bool trailing_separator=false)
Concatenate elements in the range [first, last) to a std::string.
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
std::string & rtrim(std::string &str, const std::string &chars)
trim white space or other characters from end of string
this header is dependency free
Helper functions to process strings.