Need some help trying to write a way to properly format numbers by adding "," to numbers. Ex:
1000000 -> 1,000,000
30000000000000 -> 30,000,000,000,000
In the usual case, its an easy problem. But it gets tricky in my case, since I am working with numbers up to 30 digits, unable to store them in any int, long, long long. Due to this obstacle, the user inputs a number and each digit is stored in my array individually.
I need these commas to print as I am cout the array. This means I only have array length to work with.
I want to use modulus, but I'm not sure how this would work.
I have:
1 2 3 4 5 6 7 8 9 10
|
for(int i=0; i <= arrSize; i++){
remainingArr--;
cout << Array[i];
if (counter%3 == 0) {
cout << ",";
}counter++;
}
cout << endl;
}
|
This works, but only in the millions scenario.
This means 10000000 -> 1,000,000,0.
So my question: What conditions can I add that will detect if I need to skip the comma in the case of a 10 Million/Billion case, as well as skip the comma at the end (1,000,000,)?
Thanks
- Tom