how to assigned comma and period in a thousand number?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 12
Reputation: hoceandress is an unknown quantity at this point 
Solved Threads: 1
hoceandress hoceandress is offline Offline
Newbie Poster

how to assigned comma and period in a thousand number?

 
0
  #1
Sep 11th, 2007
how to assigned comma and period in thousand number?? (ex. 1,000.50) in c++..

this is my sample code...

  1. #include <iostream>
  2.  
  3. main ()
  4. {
  5. int x=0,y;
  6.  
  7. cout<<"Coverting Dollar to Philippine Currency:";
  8. cout<<"\n\nEnter amount of dollar: $ ";
  9. cin>>y;
  10. x = y * 45.00;
  11.  
  12. cout<<"Php "<<x;
  13. return 0;
  14. }

i want to assigned comma in thousand number and period of decimal.
ex. Php 1,000.50
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: how to assigned comma and period in a thousand number?

 
0
  #2
Sep 11th, 2007
  1. #include <iostream>
  2. #include <locale>
  3. #include <iomanip>
  4. using namespace std ;
  5.  
  6. struct comma_facet : public std::numpunct<char>
  7. { protected: string do_grouping() const { return "\003" ; } };
  8.  
  9. int main()
  10. {
  11. cout.imbue( locale( cout.getloc(), new comma_facet ) ) ;
  12. cout << fixed << setprecision(2) << showpoint << 123.0 << '\t'
  13. << 12345.6789 << '\t' << 2000.0/3.0 << '\t' << 1234567.1234 << '\n' ;
  14. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 12
Reputation: hoceandress is an unknown quantity at this point 
Solved Threads: 1
hoceandress hoceandress is offline Offline
Newbie Poster

Re: how to assigned comma and period in a thousand number?

 
0
  #3
Sep 11th, 2007
i didn't get it!!!is there anything else that can easily to understand for beginners like me??
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: how to assigned comma and period in a thousand number?

 
0
  #4
Sep 11th, 2007
  1. #include <iostream>
  2. #include <sstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. ostringstream stm ;
  10. stm << fixed << setprecision(2) << showpoint << 12345678.9876 ;
  11. string str = stm.str() ;
  12. int n = str.size() ;
  13. for( int i = n-6 ; i>0 ; i -= 3 ) str.insert( i, 1, ',' ) ;
  14. cout << str << '\n' ;
  15. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,673
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: how to assigned comma and period in a thousand number?

 
0
  #5
Sep 11th, 2007
std C/C++ numerical types don't recognize comma separaters. So, as vijayan has demostrated, you need to find a way to convert the numerical value to a string, then insert the commas and print out the string. There are other ways to do this basic sequence beyond what vijayan has demonstrated, but which way is best for you will depend on what tools you have in your tool chest. To better help us help you we need to know what tools you have available. Do you know about the different types of strings? If so which do you use? Do you know about manipulating arrays/vectors? Do you know about the Standard Template Library, etc. Did you know that main() returns an int in it's standard form? Did you know that you must somehow invoke namespace std when using cout with ith iostream header file? Do you know how to convert numerical values into strings?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 12
Reputation: hoceandress is an unknown quantity at this point 
Solved Threads: 1
hoceandress hoceandress is offline Offline
Newbie Poster

Re: how to assigned comma and period in a thousand number?

 
0
  #6
Sep 11th, 2007
To Lerner;

Do you know about the different types of strings?
- not really
If so which do you use? Do you know about manipulating arrays/vectors? - i know array a litle
the rest i don't know i am not sure..
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,673
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: how to assigned comma and period in a thousand number?

 
1
  #7
Sep 11th, 2007
All right. First things first.

It should always be int main(), not void main() or plain main(). void main() and plain old main() may work on some compilers, but it's not standard. See vijayan's examples.

If you are going to use the standard iostream header file instead of the (deprecated?) iostream.h header file then you need to inovke namespace std somehow. You can do that by including the line

using namespace std;

right after the list of includes. While this isn't the most sophisticated way to access material in namespace std, it's simple, straightforward, and good enough for most purposes, particularly for beginners. See vijayan's examples.

In the final analysis strings are null terminated char arrays. That means that there must be a null char in the char array just after the last char you want to visualize. This basic type string is often called a C style string because it's the only type string you can use in C. In C++ you can still use that style string, but C++ has a standard string class that can be used that does a lot of the grunt work for you that you have to do on your own if you use C style strings. Technically this string class is part to the Standard Template Library and is sometimes called an STL string or just a C++ string to differentiate it from a C style string. This string class has an embedded C style string in it as a data member and it can be accessed as such when you need to do so. This string class is in namespace std and can be accessed by including the string header file and using some technique to gain access to namespace std. (see vijayan's examples) There is a learning curve in terms of using either type string, but more and more people are recommended beginners in C++ learn how to use the C++ style strings right from the start.

To see how to use the C++ string class, see vijayan's example. If you aren't allowed to use this string class and must use the C style strings, then so be it.

In his second example, vijayan takes advantage of some of the methods available in the string class to insert the commas. In his first example he makes extensive use of features available in C++ that will likely cost you points if you submit such a method to an instructor in a beginners class.

To start with I'd suggest you write a program using standard code that creates a string, assigns the value 12345.67 to it and prints the string to the screen. Post it once you've got it completed and then someone can help you figure out how to insert comma's in it if you don't want to use vijayan's example.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: how to assigned comma and period in a thousand number?

 
0
  #8
Sep 11th, 2007
the brute force approach:
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cassert>
  4. using namespace std;
  5.  
  6. void print_with_comma( double value )
  7. {
  8. assert( value >= 0.0 ) ;
  9. double intp ;
  10. double fracp = modf( value, &intp ) ;
  11. if( fracp >= 0.995 ) { fracp = 0.0 ; intp += 1.0 ; }
  12. int before_decimal = int( intp + 0.5 ) ;
  13. int millions = before_decimal / 1000000 ;
  14. int thousands = ( before_decimal / 1000 ) % 1000 ;
  15. int units = before_decimal % 1000 ;
  16. if( millions > 0 ) cout << millions << ',' ;
  17. if( millions>0 || thousands>0 ) cout << thousands << ',' ;
  18. cout << units << '.' ;
  19. cout.width(2) ;
  20. cout.fill('0') ;
  21. cout << int( fracp*100 + 0.5 ) << '\n' ;
  22. }
Last edited by vijayan121; Sep 11th, 2007 at 1:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 12
Reputation: hoceandress is an unknown quantity at this point 
Solved Threads: 1
hoceandress hoceandress is offline Offline
Newbie Poster

Re: how to assigned comma and period in a thousand number?

 
0
  #9
Sep 12th, 2007
i'm using the standard header file!!the using namespace std;
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,602
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: how to assigned comma and period in a thousand number?

 
0
  #10
Sep 12th, 2007
>i'm using the standard header file!!the using namespace std;
Good for you?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC