Hello everyone,
I was writing some code for a C++ class, and I am curious if there is any way to add a comma into your output? For example, let's say one calculates the amount of revenue from a tax percent and a high amount in sales. And let's say the output amount is into the millions. How would I add a comma to separate the digits for easy readability?
One method would be to turn the output value into a string, and then starting at the back, insert a comma every X value (I would guess you'd like very third value, that's quite popular) until you reach the start of the string. Then output the string.
Using locale specific formats are best used only when outputting information, otherwise store the data in a "raw" format:
Data files containing locale-specific formats are frequently misinterpreted when transferred to a system in a different locale. For example, a file containing numbers in a French format is not useful to a U.K.-specific program.
Create and imbue a new facet by deriving from std::numpunct<char>.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <locale>
#include <string>
struct group_thousands : std::numpunct<char>
{ std::string do_grouping() const override { return"\3"; } };
int main()
{
// NOTE(mbozzi): ownership of the newly-allocated facet is assumed by the library
// Typically, the following idiom is used:
std::cout.imbue(std::locale(std::cout.getloc(), new group_thousands));
// Output might be grouped with a character other than a comma,
// depending on the current locale
std::cout << 1234567890ll << '\n'; // 1,234,567,890
}
@Handy Andy,
that won't work on a system whose's native locale doesn't group by thousands.
For example, on stacked-crooked.com, to get the expected output, I need to override the locale category to something that separates thousands by default. I can do this by setting the environment variable LC_ALL to en_US.utf8, for example.
@Handy Andy, I believe that using the native locale is in many cases the best choice for data being presented to the user. In fact, yours might be the best suggestion:
If OP wants to view localized output, they can adjust the native locale to one that provides it by default.
@Furry Guy has it right to suggest using the classic locale for data that's going to be processed by another computer program.