• Articles
• format integers with commas separator us     ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091`` ``````/* by Angel Rapallo format an integer with up to 20 digits length with commas separators for hundreds, thoushands, etc. this function only uses chars not strings and does not use any locale or external library except the _i64toa_s api to convert the integer into an array of chars. it is a flat algorithm, is does not use recursion or any other method. at its worst case it can iterate around 24 times only. for example if the number has 20 digits (MAXIMUM it can handle) then it will iterate 20/4 = 5 iterations + 20 to copy the digits a total os 24 iterations. the algorithm is convert number to array[] get number of digits aka the length of the array get the position of the first comma get the total length of the formatted array digits + # of commas. create dynamic array with this information to hold the formatted number. put the commas in place for example if number is 11111 then [][,][][][][] the put the digits in place [,] as allways there must be a shorter and better way, but i havent found it so i came up with this one. one way is to use two stacks and push the digits into it then pop all the digits and include the commans using the % operator to check if a comma belongs after the digits. i like this one because it does not use anything except chars. the idea of placing commas into a number seems simple enough just go one character at a time and check if a comma belongs there the problem is that to do that you would need a way to insert chars into an array, which is what this algorith does but into steps instead of one. I hope it can help some one. */ char* format_integer_with_commas(long long n) { /* convert number to an array of chars */ char _number_array = {'\0'}; _i64toa_s(n,_number_array,sizeof(_number_array),10); char* _number_pointer = _number_array; int _number_of_digits = 0; while (*(_number_pointer + _number_of_digits++)); --_number_of_digits; /* count the number of digits calculate the position for the first comma separator calculate the final length of the number with commas the starting position is a repeating sequence 123123... which depends on the number of digits the length of the number with commas is the sequence 111222333444... */ int _starting_separator_position = _number_of_digits < 4 ? 0 :_number_of_digits % 3 == 0 ? 3:_number_of_digits % 3; int _formatted_number_length = _number_of_digits + _number_of_digits / 3 - (_number_of_digits % 3 == 0 ? 1:0); /* create formatted number array based on calculated information. */ char* _formatted_number = new char[_formatted_number_length]; /* place all the commas */ for (int i = _starting_separator_position; i < _formatted_number_length - 3; i += 4) _formatted_number[i] = ','; /* place the digits */ for (int i = 0, j = 0; i < _formatted_number_length; i++) if (_formatted_number[i] != ',') _formatted_number[i] = _number_pointer[j++]; /* close the string */ _formatted_number[_formatted_number_length] = '\0'; return _formatted_number; } ``````