how to assigned comma and period in thousand number?? (ex. 1,000.50) in c++..

this is my sample code...

#include <iostream>

main ()
{
      int x=0,y;
      
     cout<<"Coverting Dollar to Philippine Currency:";
     cout<<"\n\nEnter amount of dollar: $ ";
     cin>>y;
     x = y * 45.00;

     cout<<"Php "<<x;
     return 0;
}

i want to assigned comma in thousand number and period of decimal.
ex. Php 1,000.50

#include <iostream>
#include <locale>
#include <iomanip>
using namespace std ;

struct comma_facet : public std::numpunct<char>
{ protected: string do_grouping() const { return "\003" ; } };

int main()
{
  cout.imbue( locale( cout.getloc(), new comma_facet ) ) ;
  cout << fixed << setprecision(2) << showpoint << 123.0 << '\t'
       << 12345.6789 << '\t' << 2000.0/3.0 << '\t' << 1234567.1234 << '\n' ;
}

i didn't get it!!!is there anything else that can easily to understand for beginners like me??

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
   ostringstream stm ;
   stm << fixed << setprecision(2) << showpoint << 12345678.9876 ;
   string str = stm.str() ;
   int n = str.size() ;
   for( int i = n-6 ; i>0 ; i -= 3 ) str.insert( i, 1, ',' ) ;
   cout << str << '\n' ;
}

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?

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..

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.

commented: great post! +6

the brute force approach:

#include <iostream>
#include <cmath>
#include <cassert>
using namespace std;

void print_with_comma( double value )
{
  assert( value >= 0.0 ) ;
  double intp ;
  double fracp = modf( value,  &intp ) ;
  if( fracp >= 0.995 ) { fracp = 0.0 ; intp += 1.0 ; }
  int before_decimal = int( intp + 0.5 ) ;
  int millions = before_decimal / 1000000 ;
  int thousands = ( before_decimal / 1000 ) % 1000 ;
  int units = before_decimal % 1000 ;
  if( millions > 0 ) cout << millions << ',' ;
  if( millions>0 || thousands>0  ) cout << thousands << ',' ;
  cout << units << '.' ;
  cout.width(2) ;
  cout.fill('0') ;
  cout << int( fracp*100 + 0.5 ) << '\n' ;
}

i'm using the standard header file!!the using namespace std;

>i'm using the standard header file!!the using namespace std;
Good for you?

fixed a couple of bugs (brute force version).

#include <iostream>
#include <cmath>
#include <cassert>
#include <iomanip>
using namespace std;

void print_with_comma( double value )
{
  assert( value >= 0.0 ) ;
  double intp ;
  double fracp = modf( value, & intp ) ;
  if( fracp >= 0.995 ) { fracp = 0.0 ; intp += 1.0 ; }
  int before_decimal = int( intp + 0.5 ) ;
  int millions = before_decimal / 1000000 ;
  int thousands = ( before_decimal / 1000 ) % 1000 ;
  int units = before_decimal % 1000 ;
  bool started = millions > 0 ;
  if(started) cout << millions << ',' ;
  if( started || thousands>0  )
  {
    cout << setw( started ? 3 : 0 ) << setfill('0') << thousands << ',' ;
    started = true ;
  }
  cout << setw( started ? 3 : 0 ) << setfill('0') << units << '.' ;
  cout << setw(2) << setfill('0') << int( fracp*100 + 0.5 ) << '\n' ;
}

im using old version of C++ i wold like to share i insert comma to separate the thousand,

# inclued <iostream.h>
# include <conio.h>

main()
{
int thou;
float x,hund;

clrscr();

x=2456.23;

thou=x/1000;
hund=x-(thou*1000.00);
cout.precision(4);

cout<<"Php "<<thou<<","<<hund;
getch();
}

commented: Thanks -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.