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

  1. #1
    Registered User Korben's Avatar
    Join Date
    Aug 2012
    Location
    Seattle, WA
    Posts
    7

    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. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Lookup printf function
    Lookup modulus operator %

    That should get you started.

    Tim S.
    Last edited by stahta01; 08-08-2012 at 04:18 PM. Reason: grammar
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

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

    Soma

  4. #4
    Registered User Korben's Avatar
    Join Date
    Aug 2012
    Location
    Seattle, WA
    Posts
    7
    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. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #7
    Registered User Korben's Avatar
    Join Date
    Aug 2012
    Location
    Seattle, WA
    Posts
    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. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Korben View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Typing decimal number and cover it binary number!
    By Kazumi in forum C Programming
    Replies: 32
    Last Post: 04-16-2011, 07:21 PM
  2. Replies: 4
    Last Post: 06-28-2010, 03:13 PM
  3. Number of digits in a decimal number
    By maverix in forum C Programming
    Replies: 7
    Last Post: 11-04-2007, 12:12 PM
  4. How to set a number of decimal spaces?
    By Diablo02 in forum C# Programming
    Replies: 2
    Last Post: 09-26-2007, 02:10 PM
  5. How to get the decimal part of a number...
    By Caldus in forum C++ Programming
    Replies: 13
    Last Post: 06-12-2006, 06:41 PM