These functions work in C++, for numbers in string, with or without decimals.
Next function not support negative string numbers or decimal separators, but it was very simple:
std::string quickAddThousandSeparators(std::string value, char thousandSep = ',')
{
int len = value.length();
int dlen = 3;
while (len > dlen) {
value.insert(len - dlen, 1, thousandSep);
dlen += 4;
len += 1;
}
return value;
}
Next function support negative string numbers and decimal separators:
std::string addThousandSeparators(std::string value, char thousandSep = ',', char decimalSep = '.', char sourceDecimalSep = '.')
{
int len = value.length();
int negative = ((len && value[0] == '-') ? 1: 0);
int dpos = value.find_last_of(sourceDecimalSep);
int dlen = 3 + (dpos == std::string::npos ? 0 : (len - dpos));
if (dpos != std::string::npos && decimalSep != sourceDecimalSep) {
value[dpos] = decimalSep;
}
while ((len - negative) > dlen) {
value.insert(len - dlen, 1, thousandSep);
dlen += 4;
len += 1;
}
return value;
}
And gtest passed tests:
TEST (quickAddThousandSeparators, basicNumbers) {
EXPECT_EQ("", quickAddThousandSeparators(""));
EXPECT_EQ("1", quickAddThousandSeparators("1"));
EXPECT_EQ("100", quickAddThousandSeparators("100"));
EXPECT_EQ("1,000", quickAddThousandSeparators("1000"));
EXPECT_EQ("10,000", quickAddThousandSeparators("10000"));
EXPECT_EQ("100,000", quickAddThousandSeparators("100000"));
EXPECT_EQ("1,000,000", quickAddThousandSeparators("1000000"));
EXPECT_EQ("1,000,000,000", quickAddThousandSeparators("1000000000"));
EXPECT_EQ("1,012,789,345,456,123,678,456,345,809", quickAddThousandSeparators("1012789345456123678456345809"));
}
TEST (quickAddThousandSeparators, changeThousandSeparator) {
EXPECT_EQ("", quickAddThousandSeparators("",'.'));
EXPECT_EQ("1", quickAddThousandSeparators("1",'.'));
EXPECT_EQ("100", quickAddThousandSeparators("100", '.'));
EXPECT_EQ("1.000", quickAddThousandSeparators("1000", '.'));
EXPECT_EQ("1.000.000.000", quickAddThousandSeparators("1000000000", '.'));
EXPECT_EQ("1.012.789.345.456.123.678.456.345.809", quickAddThousandSeparators("1012789345456123678456345809", '.'));
}
TEST (addThousandSeparators, basicNumbers) {
EXPECT_EQ("", addThousandSeparators(""));
EXPECT_EQ("1", addThousandSeparators("1"));
EXPECT_EQ("100", addThousandSeparators("100"));
EXPECT_EQ("1,000", addThousandSeparators("1000"));
EXPECT_EQ("10,000", addThousandSeparators("10000"));
EXPECT_EQ("100,000", addThousandSeparators("100000"));
EXPECT_EQ("1,000,000", addThousandSeparators("1000000"));
EXPECT_EQ("1,000,000,000", addThousandSeparators("1000000000"));
EXPECT_EQ("1,012,789,345,456,123,678,456,345,809", addThousandSeparators("1012789345456123678456345809"));
}
TEST (addThousandSeparators, negativeBasicNumbers) {
EXPECT_EQ("", addThousandSeparators(""));
EXPECT_EQ("-1", addThousandSeparators("-1"));
EXPECT_EQ("-100", addThousandSeparators("-100"));
EXPECT_EQ("-1,000", addThousandSeparators("-1000"));
EXPECT_EQ("-10,000", addThousandSeparators("-10000"));
EXPECT_EQ("-100,000", addThousandSeparators("-100000"));
EXPECT_EQ("-1,000,000", addThousandSeparators("-1000000"));
EXPECT_EQ("-1,000,000,000", addThousandSeparators("-1000000000"));
EXPECT_EQ("-1,012,789,345,456,123,678,456,345,809", addThousandSeparators("-1012789345456123678456345809"));
}
TEST (addThousandSeparators, changeThousandSeparator) {
EXPECT_EQ("", addThousandSeparators("",'.'));
EXPECT_EQ("-1", addThousandSeparators("-1",'.'));
EXPECT_EQ("100", addThousandSeparators("100", '.'));
EXPECT_EQ("-1.000", addThousandSeparators("-1000", '.'));
EXPECT_EQ("-1.000.000.000", addThousandSeparators("-1000000000", '.'));
EXPECT_EQ("1.012.789.345.456.123.678.456.345.809", addThousandSeparators("1012789345456123678456345809", '.'));
}
TEST (addThousandSeparators, changeDecimalSeparator) {
EXPECT_EQ("0,5", addThousandSeparators("0.5",'.',','));
EXPECT_EQ("0", addThousandSeparators("0",'.',','));
EXPECT_EQ("-1,23", addThousandSeparators("-1.23",'.',','));
EXPECT_EQ("100,56", addThousandSeparators("100.56", '.',','));
EXPECT_EQ("-1.000,786", addThousandSeparators("-1000.786", '.',','));
EXPECT_EQ("-1.000.000.000,4859", addThousandSeparators("-1000000000.4859", '.', ','));
EXPECT_EQ("1.012.789.345.456.123.678.456.345.809,6785960", addThousandSeparators("1012789345456123678456345809.6785960", '.',','));
}
TEST (addThousandSeparators, changeSourceDecimalSeparator) {
EXPECT_EQ("0,5", addThousandSeparators("0,5",'.',',',','));
EXPECT_EQ("0", addThousandSeparators("0",'.',',',','));
EXPECT_EQ("-1,23", addThousandSeparators("-1,23",'.',',',','));
EXPECT_EQ("100,56", addThousandSeparators("100,56", '.',',',','));
EXPECT_EQ("-1.000,786", addThousandSeparators("-1000,786", '.',',',','));
EXPECT_EQ("-1.000.000.000,4859", addThousandSeparators("-1000000000,4859", '.', ',',','));
EXPECT_EQ("1.012.789.345.456.123.678.456.345.809,6785960", addThousandSeparators("1012789345456123678456345809,6785960", '.',',',','));
}
GetNumberFormat
is the way to go. – Myramyrahprintf
, C seems to be your focus. Said that: what have you found out? You should know we are not a coding service. What did you try yourself? Show your code and state wht did not work. Sad enough this has to be said. – Torus'
like so:123'456'789
. It works since C++14 and could be placed anywhere between digits, even like1'2'3'4
. – Kiowa