MCPcopy Create free account
hub / github.com/Tablecruncher/tablecruncher / groupedIntToString

Method groupedIntToString

src/helper.cpp:109–126  ·  view source on GitHub ↗

create a grouped string from the number `num` s is managed by the calling code, we assume there are `max_len` bytes available 123 => 123 123 => 321 => 1234 => 1,234 12345 => 12,345 123456 => 123,456 1234567 => 1,234,567 */

Source from the content-addressed store, hash-verified

107 1234567 => 1,234,567
108*/
109std::string Helper::groupedIntToString( int num, std::string sep ) {
110 // Caution: Only works for pure 7bit-ASCII text!
111 std::string ret = "";
112 std::string num_sign = "";
113 if( num < 0 ) {
114 num_sign = "-";
115 num = -num;
116 }
117 std::string num_str = std::to_string(num);
118 std::reverse(num_str.begin(), num_str.end());
119 for( size_t i = 0; i < num_str.length(); ++i ) {
120 ret.insert(0, 1, num_str[i]);
121 if( i % 3 == 2 && i > 0 && i < num_str.length() - 1 ) {
122 ret.insert(0, sep);
123 }
124 }
125 return num_sign + ret;
126}
127
128
129

Callers

nothing calls this directly

Calls 4

to_stringFunction · 0.85
beginMethod · 0.45
endMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected