Thread: inputting commas

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    inputting commas

    Hello,
    I'm making this program where the user enters a string for a dollar amount: "$1, 234.56" for example...

    Then I'm supposed to take that string and convert it into an actual number. All well and good (I think)...but I was wondering if 2 things were possible (and if they are, perhaps a good swift kick in the right direction could be pleasantly helpful)...

    1.) Is it possible that when I display the dollar amount as a type long double, I can make it so there are commas for every 3 digits? I couldnt figure out a way to put them in...

    2.) Is it possible when crawling through the string and finding a period, to insert a period in the numerical version?

    Thanks a lot! Chap...

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    #include <iomanip>
    double num = 3;
    cout << showpoint << num;
    That will make the decimal point show up in the output.

    Is it possible that when I display the dollar amount as a type long double, I can make it so there are commas for every 3 digits? I couldnt figure out a way to put them in...
    It wouldn't be too hard to write a function that would do that provided you know about the % operator.
    Last edited by joshdick; 04-13-2005 at 01:11 PM.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It's something of an advanced topic, but there are facilities built into the language that allow this. For more info you should read up on facets and locales. Here is an example:

    Code:
    #include <iostream>
    #include <iterator>
    #include <locale>
    #include <string>
    
    int main()
    {
        std::locale loc(std::locale("American_USA.1252"),
                        new std::money_put<char,std::back_insert_iterator<std::string> >);
        std::basic_ios<char> str(0);
        str.imbue(loc);
        str.setf(std::ios::showbase);
        std::string s = "";
    
        std::use_facet<std::money_put<char,std::back_insert_iterator<std::string> > >(loc)
            .put(std::back_inserter(s),false,str,' ',(long double)12345678);
        std::cout << s << std::endl;
    
        return 0;
    }
    Output:
    Code:
    $123,456.78
    This may or may not work depending on how well your compiler supports these features. The above example works on my MS Visual C++ .NET compiler but not my MS Visual C++ 6.0 compiler.

    The easy route for you might involve using stringstreams to convert a numerical value into a string and then manipulating that string, inserting commas in the appropriate locations based on the length of the string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    I'm not exactly sure what showpoint is doing...here's how I'm using it...

    Code:
    for (i = 0; i < length; i++)
    	{
    		if (mon_str.at(i) >= '0' && mon_str.at(i) <= '9')
    			dollars = dollars * 10 + (mon_str.at(i)) - '0';
    		if (mon_str.at(i) == '.')
    			cout << showpoint;
    	}
    Clearly this is not the proper way to use it...what do I seem to be missing here...(besides a lot :-P) ?

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    double num = 3;
    cout << showpoint << num;
    Put that in an empty project and take note of the output. It'll probably be something like 3.000000

    Since you just want to output money, you'll probably want to setprecision(2) so that outputting 3 would show up in output as 3.00

    Then, all you have to do is output a $ first and you've got something that looks like money. That doesn't take care of comma separators, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking for commas in imported files
    By Ness757 in forum C Programming
    Replies: 2
    Last Post: 03-23-2006, 01:49 PM
  2. to add commas
    By sweetly in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2003, 01:03 PM
  3. Problems with commas as input
    By Yojimbo III in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2003, 09:18 PM
  4. Inserting commas using recursion
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2002, 10:15 AM
  5. commas in large numbers
    By HKR in forum C Programming
    Replies: 7
    Last Post: 03-06-2002, 07:08 PM