# How to format a decimal number as US currency without locales?

This is a discussion on How to format a decimal number as US currency without locales? within the C Programming forums, part of the General Programming Boards category; Hello, I'm new to programming and I'm having a hard time with what one would assume would be simple. But ...

1. ## How to format a decimal number as US currency without locales?

Hello, I'm new to programming and I'm having a hard time with what one would assume would be simple. But without using locales, how do you format a decimal number, like 30498503.483 into \$30,498,503.48? Basically I want to store a decimal value in a variable, and then convert the contents of the variable into US currency and print it to the screen. I do not need any built-in currency converters like locales, just need to learn how to format a numeric decimal value into a currency string and print it to the screen.

Thank you for any help on this, I appreciate your time.

2. Lookup printf function
Lookup modulus operator %

That should get you started.

Tim S.

3. O_o

Standard warning: floating point values are really not the way to go for monies.

Soma

4. I'm taking a very large amount of pennies, stored in a Long Long variable type, and dividing it by 100. So my plan is to then convert that value into currency and print it to the screen.

5. Following that method, printing without commas is relatively straightforward.

If you want to incorporate commas, then it would be a bit more challenging. Have you had any exposure to arrays and/or strings?

6. We had a fun bit of comparison with several ways to do this, in the forum. Unfortunately, it gets pushed back far enough and it all disappears from our view.

You can do this by working with bits, working with arithmetic, or working dynamically with a string.

I'll describe the dynamic way. You have a function:
Code:
```void printWithCommas(int num) { //your parameter would be a long
int i, j, len;
char ch;
char c[SIZE+10]; //where SIZE is a #define SIZE 10,20,whatever.
/* c will handle up to 9 commas in a number */

i=0;
j=1; //j simplifies some logic in the while loop
while(num > 0) {
c[i]= num % 10 + '0';
/*
The +'0', makes the int digit, print out as a char with %c.
Without it, you'll see weird char's: faces, cards, etc.

Num % 10 gets the value of the rightmost digit of num
because we use a base 10 numbering system. You knew
there would be *some* arithmetic in here. ;)

*/
num /= 10;   //remove the rightmost digit from the number
++i;
if(j%3==0 && num>0) {
c[i++]=',';
}
++j;
}
/* Add the end of string char ('\0', to c at c[i].

Now you have the answer but it's in reverse order.
So either reverse it (or print it char by char in reverse order),
and print it, and you're done.
*/

}```
I'm posting this because it's difficult to describe this way of doing it, and make it understandable, to those who have not been exposed to it. At least, not if you attended public schools.

The arithmetic way of doing this is straightforward, but boring.

7. Thanks guys for the hints. I'm going to read up more on printf and modulo (I do know about using modulo division for finding odd/even numbers, but I'm assuming you meant something else?). Adak, thank you for the start, I honestly don't understand parts of whats going on there, but some of it I do. I'm going to dig into it and figure it out, thanks again.

8. Originally Posted by Korben
Thanks guys for the hints. I'm going to read up more on printf and modulo (I do know about using modulo division for finding odd/even numbers, but I'm assuming you meant something else?). Adak, thank you for the start, I honestly don't understand parts of whats going on there, but some of it I do. I'm going to dig into it and figure it out, thanks again.
You're welcome. The mod operator, when used on an integer, (onesColumnValue = number mod 10), yields the value of the digit in the one's column of the number.

So it's very useful here, since we are "peeling off" the digits from the right hand side, one at a time.