424,852 Members | 1,014 Online
Need help? Post your question and get tips & solutions from a community of 424,852 IT Pros & Developers. It's quick & easy.

# how to display 12345 as 12,345 ??

 P: n/a suppose i have: int price1 = 35000; int price2 = 600; int price3 = 8765; int price4 = 120000; int price5 = 3800000; and i want to output to screen the following: 35,000 600 8,765 120,000 3,800,000 what should i do ?? any good function to do this ?? Jul 22 '05 #1
23 Replies

 P: n/a Użytkownik "news.hku.hk" napisał w wiadomo¶ci news:40******@newsgate.hku.hk... suppose i have: int price1 = 35000; int price2 = 600; int price3 = 8765; int price4 = 120000; int price5 = 3800000; and i want to output to screen the following: 35,000 600 8,765 120,000 3,800,000 what should i do ?? any good function to do this ?? Not in the standard. You'll have to do it yourself. I believe it is quite simple. regards, Marcin Jul 22 '05 #2

 P: n/a "news.hku.hk" wrote in message news:40******@newsgate.hku.hk... suppose i have: int price1 = 35000; int price2 = 600; int price3 = 8765; int price4 = 120000; int price5 = 3800000; and i want to output to screen the following: 35,000 600 8,765 120,000 3,800,000 what should i do ?? any good function to do this ?? You could look up facets, and check out Dinkumware. Jul 22 '05 #3

 P: n/a I've done this before. I've adapted it for the purpose of this post, but here goes (Check for bugs before you use it!): char szBuffer[30]; //Global Variable char* GenerateString(unsigned long); int main(void) { cout << GenerateString(12345); } char* GenerateString(unsigned long Numbr) { szBuffer[29] = '\0'; char* pChar = &m_szBuffer[28]; unsigned long char_counter = 0; unsigned long comma_counter = 0; unsigned long nResult; //will be initialized unsigned long nTempValue = Numbr; for(;;) { nResult = 0; //I used to use the method below, but it's FAR too slow /* while ( nTempValue >= 10 ) { nTempValue -= 10; nResult += 1; }*/ nResult = nTempValue / 10; nTempValue %= 10; *pChar = ( ( '0' ) + ( nTempValue ) ); pChar -= 1; char_counter +=1; nTempValue = nResult; //Very Important! if (!nTempValue) break; //This line prevents ",345" being generated char_counter += 1; if ( comma_counter == 3 ) { *pChar = ','; pChar -= 1; char_counter += 1; comma_counter = 0; } } //End of "For" return pChar; } Hope that helps. Also, you may try incorporating it into a class ( I did! ) and also overide the ostream "<<" operator! That'd be dead handy. I'd post the class I wrote for it but it had a very specific purpose other than just genrating these strings. -JKop Jul 22 '05 #4

 P: n/a "news.hku.hk" wrote in message news:<40******@newsgate.hku.hk>... suppose i have: int price1 = 35000; int price2 = 600; int price3 = 8765; int price4 = 120000; int price5 = 3800000; and i want to output to screen the following: 35,000 600 8,765 120,000 3,800,000 what should i do ?? any good function to do this ?? Hi, I am new to C++ too so my solution might not be very good. Anyway, I will try something like: void _display_number(int v, int n){ if(v >= 1000){ int r = v % 1000; _display_number(v / 1000,n); printf(",%03d",r); // how to translate that into std::cout<<...? }else{ printf("%s%d\n",n ? "-":"",v); } } void display_number(int v){ _display_number(v < 0 ? -v : v,v < 0); } int main(int argc,char** argv){ display_number(35000); display_number(600); display_number(8765); display_number(120000); display_number(3800000); display_number(-3800000); } The program prints something like: 35,000 600 8,765 120,000 3,800,000 -3,800,000 HTH. I am here to learn as well so if anyone can help me improve the program, I am happy to see it. Thanks! Jul 22 '05 #5

 P: n/a "JKop" wrote in message news:CU******************@news.indigo.ie... I've done this before. I've adapted it for the purpose of this post, but here goes (Check for bugs before you use it!): char szBuffer[30]; //Global Variable char* GenerateString(unsigned long); int main(void) { cout << GenerateString(12345); } char* GenerateString(unsigned long Numbr) { szBuffer[29] = '\0'; char* pChar = &m_szBuffer[28]; unsigned long char_counter = 0; unsigned long comma_counter = 0; unsigned long nResult; //will be initialized unsigned long nTempValue = Numbr; for(;;) { nResult = 0; //I used to use the method below, but it's FAR too slow /* while ( nTempValue >= 10 ) { nTempValue -= 10; nResult += 1; }*/ nResult = nTempValue / 10; nTempValue %= 10; *pChar = ( ( '0' ) + ( nTempValue ) ); pChar -= 1; char_counter +=1; nTempValue = nResult; //Very Important! if (!nTempValue) break; //This line prevents ",345" being generated char_counter += 1; if ( comma_counter == 3 ) { *pChar = ','; pChar -= 1; char_counter += 1; comma_counter = 0; } } //End of "For" return pChar; } Hope that helps. Also, you may try incorporating it into a class ( I did! ) and also overide the ostream "<<" operator! That'd be dead handy. I'd post the class I wrote for it but it had a very specific purpose other than just genrating these strings. -JKop How about (untested code, and YMMV) #include #include int main() { using namespace std; cout.imbue(locale("English")); cout << 12345 << '\n'; } ;) JE Jul 22 '05 #6

 P: n/a thanks for all of your's help but i still can't grasp the idea of the algorithm maybe could anyone explain in words(preferably with c++ codes) that how the algorithm works ?? Thanks "pembed2003" wrote in message news:db**************************@posting.google.c om... "news.hku.hk" wrote in message news:<40******@newsgate.hku.hk>... suppose i have: int price1 = 35000; int price2 = 600; int price3 = 8765; int price4 = 120000; int price5 = 3800000; and i want to output to screen the following: 35,000 600 8,765 120,000 3,800,000 what should i do ?? any good function to do this ?? Hi, I am new to C++ too so my solution might not be very good. Anyway, I will try something like: void _display_number(int v, int n){ if(v >= 1000){ int r = v % 1000; _display_number(v / 1000,n); printf(",%03d",r); // how to translate that into std::cout<<...? }else{ printf("%s%d\n",n ? "-":"",v); } } void display_number(int v){ _display_number(v < 0 ? -v : v,v < 0); } int main(int argc,char** argv){ display_number(35000); display_number(600); display_number(8765); display_number(120000); display_number(3800000); display_number(-3800000); } The program prints something like: 35,000 600 8,765 120,000 3,800,000 -3,800,000 HTH. I am here to learn as well so if anyone can help me improve the program, I am happy to see it. Thanks! Jul 22 '05 #7

 P: n/a On Mon, 26 Apr 2004 21:37:07 +0800, "news.hku.hk" wrote: suppose i have:int price1 = 35000;int price2 = 600;int price3 = 8765;int price4 = 120000;int price5 = 3800000;and i want to output to screen the following:35,0006008,765120,0003,800,000what should i do ?? any good function to do this ?? Yet another alternative. This one converts the number into a string and puts the commas into the string. Not necessarily better, just different. rossum #include #include #include #include std::string comma(int num) { // Deal with negatives if(num < 0) { return "-" + comma(std::abs(num)); } // Convert number to string std::stringstream ss; ss << num; std::string num_string = ss.str(); // Insert a comma every third position for (int i = num_string.length() - 3; i > 0; i -= 3) { num_string.insert(i, 1, ','); } // end for return num_string; } // end comma() //--------------------------- int main() { std::cout << comma(-3800000) << '\n' << comma(-120000) << '\n' << comma(-35000) << '\n' << comma(-8765) << '\n' << comma(-600) << '\n' << comma(-50) << '\n' << comma(-4) << '\n' << comma(0) << '\n' << comma(4) << '\n' << comma(50) << '\n' << comma(600) << '\n' << comma(8765) << '\n' << comma(35000) << '\n' << comma(120000) << '\n' << comma(3800000) << std::endl; return EXIT_SUCCESS; } // end main() -- The Ultimate Truth is that there is no Ultimate Truth Jul 22 '05 #8

 P: n/a "John Ericson" wrote in message news:Eijjc.1921 This is a good solution. Not sure why everyone is re-inventing the wheel. using namespace std; cout.imbue(locale("English")); cout << 12345 << '\n'; But, is there a locale named "English"? Jul 22 '05 #9

 P: n/a Thanks, i see your point but how to return to the original state?? because i just want to insert the comma in someline while later i just want to display ordinary numbers. also, from your quotation, if i add one more cout line after cout << 12345 <<'\n'; i.e. cout << 543532 << '\n'; there will be runtime error.......what's up ?? "Siemel Naran" wrote in message news:vN*********************@bgtnsc04-news.ops.worldnet.att.net... "John Ericson" wrote in message news:Eijjc.1921 This is a good solution. Not sure why everyone is re-inventing the wheel. using namespace std; cout.imbue(locale("English")); cout << 12345 << '\n'; But, is there a locale named "English"? Jul 22 '05 #10

 P: n/a "news.hku.hk" wrote in message news:408f9591 Thanks, i see your point but how to return to the original state?? because i just want to insert the comma in someline while later i just want to display ordinary numbers. Haven't tried it yet, but approaches I suggest is: (1) use 2 locales, and imbue cout with first locale when you want num_put to put comma symbols and default locale when you don't want comma symbols, (2) create a new type Amount for which operator<<(ostream&, Amount) uses num_put for Amount objects and this num_put uses comma symbols. See my response in the thread with subject "convert hex to oct" for this imbue, use_facet, etc. But there was a C++ magazine that had and maybe still has lots of articles on these ideas, and I think the articles were by Plauger, so maybe you can browse for that too. also, from your quotation, if i add one more cout line after cout << 12345 <<'\n'; i.e. cout << 543532 << '\n'; there will be runtime error.......what's up ?? No error. I don't know what quotation you're talking about. "Siemel Naran" wrote in message news:vN*********************@bgtnsc04-news.ops.worldnet.att.net... "John Ericson" wrote in message news:Eijjc.1921 This is a good solution. Not sure why everyone is re-inventing the wheel. using namespace std; cout.imbue(locale("English")); cout << 12345 << '\n'; But, is there a locale named "English"? Jul 22 '05 #11

 P: n/a news.hku.hk wrote: Thanks, i see your point but how to return to the original state?? because i just want to insert the comma in someline while later i just want to display ordinary numbers. cout.imbue(locale("C")); cout.imbue(locale("English")); But, is there a locale named "English"? Since the sample works fine for me, I would assume there is. At least for my compiler. Why not try it and see with your compiler? -- Regards, Joe Hotchkiss, http://joe.hotchkiss.com XXXXXXXXXXXXXXXXXXXXXXXXX X joe.hotchkiss X X at baesystems.com X XXXXXXXXXXXXXXXXXXXXXXXXX Jul 22 '05 #12

 P: n/a "news.hku.hk" wrote in message news:<40******@newsgate.hku.hk>... suppose i have: int price1 = 35000; int price2 = 600; int price3 = 8765; int price4 = 120000; int price5 = 3800000; and i want to output to screen the following: 35,000 600 8,765 120,000 3,800,000 what should i do ?? any good function to do this ?? You might want to look at commafmt.c at www.snippets.org. -- Later, Jerry. The universe is a figment of its own imagination. Jul 22 '05 #13

 P: n/a news.hku.hk wrote: Suppose that I have: int price1 = 35000; int price2 = 600; int price3 = 8765; int price4 = 120000; int price5 = 3800000; and that I want to output to screen the following: 35,000 600 8,765 120,000 3,800,000 What should I do? Are there any good functions to do this? cat main.cc #include #include class Price { private: // representation int I; public: // functions friend std::ostream& operator<<(std::ostream& os, const Price& i); // operators // implicit operator int(void) const { return I;} operator int(void) { return I;} // constructors Price(int i = 0): I(i) { } }; inline std::ostream& operator<<(std::ostream& os, const Price& i) { int q = (int)i/1000; int r = (int)i%1000; if (0 < q) os << Price(q) << ',' << std::setfill('0') << std::setw(3) << r; else os << r; return os; } int main(int argc, char* argv[]) { const int price[] = {35000, 600, 8765, 120000, 3800000}; const size_t n = sizeof(price)/sizeof(price[0]); for (size_t j = 0; j < n; ++j) std::cout << Price(price[j]) << std::endl; return 0; } g++ -Wall -ansi -pedantic -o main main.cc ./main 35,000 600 8,765 120,000 3,800,000 Jul 22 '05 #14

 P: n/a On 26 Apr 2004 12:30:35 -0700, pe********@yahoo.com (pembed2003) wrote: [snip OP]Hi,I am new to C++ too so my solution might not be very good. Anyway, Iwill try something like: Show what headers you are including. Code should be compilable as is, just cut and paste. void _display_number(int v, int n){ Avoid leading underscores, they are used internally by compilers so if you pick the wrong name you will get a compilation error. Since compilers differ this might also make your code non-portable. Trailing underscores are safer and hence better. So change _display_number() to display_number_(). You have a good self explanatory name for the function, but you use short cryptic names for the two parameters. Better to change v to value and n to is_negative so they are more self explanatory. The second parameter is a yes/no flag so it is better to declare is as a bool which gives a better indication of its purpose. It is possible to eliminate this parameter by taking a slightly different approach to the recursion. if(v >= 1000){ Put a space between the if and the open bracket: if (value >= 1000) {. This is to differentiate it from function calls. ^--added space int r = v % 1000; Another short cryptic variable name. Change it to something meaningful. _display_number(v / 1000,n); Better to put a space after the comma which separates the parameters. printf(",%03d",r); // how to translate that intostd::cout<<...? Better to put a space after the second comma which separates the parameters. For cout look up the I/O manipulators. }else{ printf("%s%d\n",n ? "-":"",v); The ternary operator ?: can make for unreadable code. Better to use it sparingly. If you do use it then put spaces round both the '?' and the ':' for clarity. Again spaces after any commas that separate parameters are easier to read. Are you really sure about that '\n' there? I hope that you cut-and-pasted this rather than typed it from scratch. }}void display_number(int v){ _display_number(v < 0 ? -v : v,v < 0); Where the ternary operator has a boolean expression it is better to put the expression in brackets: (v < 0) ? -v : v to improve readability. Again put a space after the parameter separating comma.}int main(int argc,char** argv){ display_number(35000); display_number(600); display_number(8765); display_number(120000); display_number(3800000); display_number(-3800000); Main does not require a return value, but it is better style to include it explicitly. I tend to use the EXIT_SUCCESS and EXIT_FAILURE macros. }The program prints something like:35,0006008,765120,0003,800,000-3,800,000HTH. I am here to learn as well so if anyone can help me improve theprogram, I am happy to see it. Thanks! Hope you find that useful. rossum -- The Ultimate Truth is that there is no Ultimate Truth Jul 22 '05 #15

 P: n/a "Siemel Naran" wrote in message news:vN*********************@bgtnsc04-news.ops.worldnet.att.net... "John Ericson" wrote in message news:Eijjc.1921 This is a good solution. Not sure why everyone is re-inventing the wheel. using namespace std; cout.imbue(locale("English")); cout << 12345 << '\n'; But, is there a locale named "English"? ;) No guarantees there - on my system there is (Win XP, VC++ 2003). I've got a few books to go before hitting Langer and Kreft's "Standard C++ IOStreams and Locales," but I'm looking forward to what looks like it's going to be a good read. - - Best regards, JE Jul 22 '05 #16

 P: n/a "Joe Hotchkiss" wrote in message news:408fee36 But, is there a locale named "English"? Since the sample works fine for me, I would assume there is. At least for my compiler. Why not try it and see with your compiler? Fair enough, but the standard does not appear to mention this. Jul 22 '05 #17

 P: n/a > Is this a rule for this newsgroup to always include only complete program as an answer and must be compilable by cut and paste? Just curious, new to this newsgroup... Its not a rule, but its obviously a good idea if you want to get help. If you submit partial code, then there is a good chance that people won't be able to answer the question because you missed out some vital piece of information. Its amazing how many posters, despite not understand why their code doesn't work are convinced that they know where the mistake is in their code. Eventually when you get them to post more code, you often find that the real error was in some part of the code they didn't originally post. If you type in code rather than cut and paste, you are liable to get your typing mistakes corrected instead of your real question answered. Given that many newbies post to this group, its often hard to tell the difference between a typing mistake and a genuine misunderstanding. john Jul 22 '05 #19

 P: n/a Just a few notes from the view of someone who has implemented this stuff... "news.hku.hk" wrote: but how to return to the original state?? If you only need an occasional switch between locales, you can just switch them by calling 'imbue()', eg.: std::locale english("English"); std::cout.imbue(english); // ... std::cout.imbue(std::locale()); The default constructor just uses a copy of the global locale (internally, locales are a reference counted collection of facets and copying them is fast). However, the 'imbue()' operation itself is relatively expensive. In particular, it is reasonable for streams to cache quite a few values obtained from a locale to improve the overall performance. As a result, you might consider to use a separate stream writing to the same destination but with a different locale, eg.: std::ostream ecout(std::cout.rdbuf()); ecout.imbue(std::locale("English")); ecout << "english: " << 10000 << "\n"; std::cout << "normal: " << 10000 << "\n"; Since creation of streams themselves is also relatively expensive, you might want to keep the additional stream around rather than creating it each time around (which would probably be more expensive than switching locales). Also, the names for the named locales are not specified by the standard. Of course, you can figure out which locales are around when porting your application to another compiler/library but for portable code you would probably create an own locale with a specialized 'std::numpunct' facet. Here is a brief code example (entirely untested, however...): #include class mynumpunct: public std::numpunct { std::string do_grouping() const { char group[] = { 3, 0 }; return group; } }; int main() { std::locale myloc(std::locale(), new mynumpunct); std::cout.imbue(myloc); // ... } Actually, you can do funny things with the grouping: it does not have to be the same amount of characters every time! For example, if 'group' would be initialized like char group[] = { 1, 2, 3, 0 }; you would get "1,234,56,7" when printing "1234567" (as an integer, that is; grouping is not applied to strings, of course). also, from your quotation, if i add one more cout line after cout << 12345 <<'\n'; i.e. cout << 543532 << '\n'; there will be runtime error.......what's up ?? This seems to be an error somewhere in the code you use: there should be no problem using 'std::cout' even with a modified locale. -- - Software Development & Consulting Jul 22 '05 #20

 P: n/a "John Harrison" wrote in message news:... Is this a rule for this newsgroup to always include only complete program as an answer and must be compilable by cut and paste? Just curious, new to this newsgroup... Its not a rule, but its obviously a good idea if you want to get help. If you submit partial code, then there is a good chance that people won't be able to answer the question because you missed out some vital piece of information. Its amazing how many posters, despite not understand why their code doesn't work are convinced that they know where the mistake is in their code. Eventually when you get them to post more code, you often find that the real error was in some part of the code they didn't originally post. Hi John, Yes I understand that but my confusion is not when people asking question, it's when people post an answer to a question. If OP asks a solution for a function to display a number in a certain format, do you normally give OP a whole program or just the function that does it? Thanks! Jul 22 '05 #21