thousand comma separator for numbers


How do I format a number to be displayed with commas to separate every three zeroes like this 1,000,000.00 in c++ for double and 1,000,0000 for integer?

I ultimately want to have this information stored in a string so I can display it in OpenGL. I already have my OpenGL code to print the string. But want an efficient method to add in the commas into the string.
Last edited on
You could look at <locale> (see the reference section of this site).

http://www.cplusplus.com/reference/locale/numpunct/thousands_sep/

(Personally, I wouldn't recommend this notation in scientific work, though.)
Last edited on
Is that the same solution in this video?

https://www.youtube.com/watch?v=9SubQGhfgJQ&t=699s

I copy pasted the code but there were still errors: c:\users\neo\downloads\workspace\c++vs6\2d\spacecannonsource\textout.h(65) : error C2664: '__thiscall std::locale::std::locale(const char *,int)' : cannot convert parameter 1 from 'class std::locale' to 'const char *'




Last edited on
Please post the code that you are having trouble with. Enough to run if the compile time errors were fixed.
Does this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <locale>
using namespace std;

struct separated : numpunct<char>
{
   char do_thousands_sep() const { return ','; }
   string do_grouping() const { return "\03"; }
};


int main()
{
    locale our_local( cout.getloc(), new separated );

    double d = 1000000;
    int i = d + 0.5;
    
    cout.imbue( our_local );
    cout << fixed << setprecision(2) << d << '\n';
    cout << i << '\n';
    
    stringstream ss;
    ss.imbue( our_local );
    ss << fixed << setprecision(2) << d << " and " << i;
    string S = ss.str(); 
    cout << "As a string: " << S;
}


1,000,000.00
1,000,000
As a string: 1,000,000.00 and 1,000,000
Last edited on
error C2664: '__thiscall std::locale::std::locale(const char *,int)' : cannot convert parameter 1 from 'class std::locale' to 'const char *'


im using vs6
Last edited on
recklessasiandriver wrote:
error C2664: '__thiscall std::locale::std::locale(const char *,int)' : cannot convert parameter 1 from 'class std::locale' to 'const char *'


Is that:
- the error when running my code?
- the error when running your code?

Please post your CODE, not the error message.
Last edited on
locale our_local( cout.getloc(), new separated );


c:\users\david\onedrive\workspace\c++vs6\2d\spacecannonsource\textout.cpp(155) : error C2664: '__thiscall std::locale::std::locale(const char *,int)' : cannot convert parameter 1 from 'class std::locale' to 'const char *'



I also shifted the code to a different part of my code and got the above error.
Show the WHOLE of your code, not one line.

We need to be able to compile it.

How old is your compiler? Run my code on this site (Click on "Edit & Run").
VS6 is, err, ancient.
Last edited on
im using vs6


All bets are off. That's just not old, it's practically pre-historic! Install the current version of VS2019 Community edition (free).
What's the link to it? I will try and upgrade my code to it.
What's the link to it?


Do you have Google? Consider using it sometime.
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <locale>

struct comma_facet : public std::numpunct<char> {
	explicit comma_facet(unsigned char group) : g(group) {}
	char do_thousands_sep() const override { return ','; }
	std::string do_grouping() const override { return std::string(1, g); }
	unsigned char g {};
};

auto set000s(std::ostream& os, unsigned char group = 3)
{
	return os.imbue(std::locale(os.getloc(), new comma_facet(group)));
}

int main()
{
	const auto num {9876543};

	set000s(std::cout);
	std::cout << num << '\n';

	set000s(std::cout, 4);
	std::cout << num << '\n';
}



9,876,543
987,6543

Last edited on
c:\users\david\onedrive\workspace\c++vs6\2d\spacecannonsource\main.cpp(7) : error C2146: syntax error : missing ';' before identifier 'override'

Adding a semi colon then creates this:

c:\users\david\onedrive\workspace\c++vs6\2d\spacecannonsource\main.cpp(7) : error C2059: syntax error : 'return'
Last edited on
What's the link to it?


Do you have Google? Consider using it sometime.

Government has forbidden it's use in our country.
Compile as C++11! Or try removing override.
Last edited on
This will compile on C++98. Not specifically tried with vs6, which may have its own peculiarities.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <locale>

struct comma_facet : public std::numpunct<char> {
	explicit comma_facet(unsigned char group) : g(group) {}
	char do_thousands_sep() const  { return ','; }
	std::string do_grouping() const  { return std::string(1, g); }
	unsigned char g;
};

std::locale set000s(std::ostream& os, unsigned char group = 3)
{
	return os.imbue(std::locale(os.getloc(), new comma_facet(group)));
}

int main()
{
	const int num = 9876543;

	set000s(std::cout);
	std::cout << num << '\n';

	set000s(std::cout, 4);
	std::cout << num << '\n';
}
If unable to use Visual Studio IDE, then consider Code::Blocks.
https://www.codeblocks.org/

The bundled compiler with C::B 20.03 isn't up-to-date, it is newer than that supplied with Bloodshed or Orwell Dev-C++.

Using a different compiler with C::B is not too difficult to set up. My C::B installs use the latest MinGW x64 version.
http://mingw-w64.org/doku.php

There are newer versions of Dev-C++ clones available.
https://royqh.net/devcpp-en/ (Red Panda Dev-C++)
https://www.embarcadero.com/free-tools/dev-cpp

I've tried both, I could get only Red Panda to install on my Win10 machines. Embarcadero's Dev-C++ stalled during the install.

If unable to use C::B or the newer clone versions of Dev-C++ realize that you are restricted to using an older C++ standard. C++ keeps changing. C++20 is now the current standard, C++23 features are already being planned.
seeplus wrote:
Compile as C++11

The last update to VS 6 was back in 2004, so C++11 isn't possible. IIRC support for C++03 was iffy even with the last update.

The OP will either need to get a newer compiler/IDE, or try Ganado's code.
Topic archived. No new replies allowed.