• Articles
  • format integers with commas separator us
Published by
Oct 25, 2012 (last update: Oct 27, 2012)

format integers with commas separator using only chars

Score: 3.1/5 (35 votes)
*****
the code has all the comments and information. this function formats an integer
using commas separators. for example 11111 becomes 11,111. it does not use anything except native chars. I hope you find it uselful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
         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
			[1][1][,][1][1][1]

	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[20] = {'\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;
}