Advertisement

printing commas in a number

Started by August 26, 2024 03:12 AM
6 comments, last by frob 3 months, 2 weeks ago

What is the best way to add commas to a number in C++?

Thanks.

Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/

Using a search engine to find the answer to that question is probably the best way.

Advertisement

What about this?

std::string add_commas(long long number) {
    std::string num_str = std::to_string(number);
    int insert_position = num_str.length() - 3;
    while (insert_position > 0) {
        num_str.insert(insert_position, ",");
        insert_position -= 3;
    }
    return num_str;
}
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/

If it works for you, that would be good enough, right?

I am not sure what kind of answer you are looking for. “this” versus what requirement or what alternative do you mean?

A judgement like “good or bad solution” depends on the requirements that you have for it. Generally, you then try to find several ways to implement the functionality, so you can compare them and pick the best solution.

Looks good. If your number is -9000 does it give you -900,0 ?

I once wrote a routine for finding the best maximum, minimum, and incremental value for an XY Plot axis. I thought it was error tight until I was showing it off to somebody. I ended up writing a unit test that would test hundreds of random numbers. This allowed me to find exceptional cases where it failed.

i found a bug by testing it like this:

#include <iostream>
#include <string>

std::string add_commas(long long number) {
    std::string num_str = std::to_string(number);
    int insert_position = num_str.length() - 3;
    while (insert_position > 0) {
        num_str.insert(insert_position, ",");
        insert_position -= 3;
    }
    return num_str;
}

int main()
{

    std::cout << add_commas(-9000) << std::endl;
    std::cout << add_commas(-900000) << std::endl;


}

it breaks for -900000

-9,000
-,900,000

Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
Advertisement

Beware: it breaks for all kinds of locales around the globe.

In much of the world the comma is treated as the decimal separator, 12,345 is about 12 and a third.

Many regions of the globe use a dot for thousands separator and comma as a decimal separator. Others use a space, others use an apostrophe. Many encoding standards use a half space character, \thinspace or U+202F. There are also variations that are different for currency than for general numbers.

So you might see:

1.234.567,89

1,234,567.89

1 234 567.89

1 234 567,89

1'234'567.89

Rather than re-invent the systems, for c++ std::numpunct has some locale-specific values, but even then on any arbitrary system and operating environment it may or may not be set correctly. You can imbue a C++ stream with any locale settings and it will automatically format it, or you can use the locale information directly, like this. You can also use OS-specific functions like GetNumberFormatEx() as well.

Often the best approach is to not use separators at all.

This topic is closed to new replies.

Advertisement