is there a manipulator in C++ that do that ?

if not how can i cout my numbers formatted that way .

if the number is larger than 999.99 i want to put a comma after the first 3 digits and after the 6 digits ..etc

x = 99456
Example : cout << X

shows in the screen : 99,456 instead of 99456 .

Dani AI

Generated

Building on 's string-based idea and 's tip, two concise, practical ways to get the output 99,456 instead of 99456 are shown below: (A) a stream-friendly locale facet (acts like a manipulator so cout prints with separators), and (B) a tiny string-based formatter (easy to adapt for decimals and negatives).

#include <iostream>
#include <locale>

struct comma_numpunct : std::numpunct<char> {
protected:
    char do_thousands_sep() const override { return ','; }
    std::string do_grouping()   const override { return "\3"; } // groups of 3
};

int main() {
    std::locale loc(std::locale::classic(), new comma_numpunct);
    std::cout.imbue(loc);                // stream will use comma grouping
    std::cout << 99456 << '\n';         // prints: 99,456
}
#include <string>

std::string format_thousands(std::string s) {
    bool neg = !s.empty() && s[0] == '-';
    if (neg) s.erase(0,1);
    int pos = (int)s.length() - 3;
    while (pos > 0) {
        s.insert(pos, ",");
        pos -= 3;
    }
    if (neg) s.insert(s.begin(), '-');
    return s;
}

// usage: format_thousands(std::to_string(x))

Notes and caveats: the custom numpunct approach integrates with iostreams and acts like a manipulator (use an ostringstream + imbue for temporary formatting). The string-insert method is simple, fast for ad-hoc formatting and easy to extend to split out a fractional part and format only the integer portion. Negative values and different grouping patterns (non-3) require small adjustments. If the platform's iostream implementation does not apply grouping for the desired types, the manual formatter is the safest portable fallback.

Recommended Answers

All 5 Replies

Read this thread

thanks a lot. i thought there is a manipulator the can do the job directly.

is this the easiest way "fixed a couple of bugs (brute force version). "

it seems too long for adding a comma for the thousand / million ranges

That is only one way to do it. A more generic way would be to first convert the int to string, then copy the digits in reverse order (from right to left) into another string adding the commas. Finally you will have to reverse the result string because it will be backwards. std::reverse() will do that.

A more generic way would be to first convert the int to string, then copy the digits in reverse order (from right to left) into another string adding the commas. Finally you will have to reverse the result string because it will be backwards. std::reverse() will do that.

Unless you copy into the string starting at the end of the destination string.

great , thanks all for the help .

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.