CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Format Thousand Grouped Numbers

1. Member
Join Date
Feb 2009
Posts
252

## Format Thousand Grouped Numbers

whats the simpler way to formar numbers with thousand separator lets say:

1000 should be 1.000 or even 1,000 would work

i rather the . instad of , for thousands since in spanish we use it backwards, comma is for decimals, and dot is for thousands.

2. Member
Join Date
Feb 2009
Posts
252

## Re: Format Thousand Grouped Numbers

Made this function and works fine
Code:
```void formatn(INT64 number, char* formated){
char CStr[60]="";
sprintf(CStr,"%d",number);
std::string str = CStr;
for (int i = str.length()-3; i > 0; i -= 3){
str.insert(i, ".");
}
strcpy(formated, str.c_str());
}```

3. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Format Thousand Grouped Numbers

Made this function and works fine
Code:
```void formatn(INT64 number, char* formated){
char CStr[60]="";
sprintf(CStr,"%d",number);
std::string str = CStr;
for (int i = str.length()-3; i > 0; i -= 3){
str.insert(i, ".");
}
strcpy(formated, str.c_str());
}```
It doesn't work if the thousands seperator is the ",".

Use the system's locale to determine the thousands seperator, and not hard code the character. There are various Windows and C++ locale functions to determine this. Or at the very least, pass the thousands seperator as another parameter.

Secondly, why are you mixing 'C' and C++ style strings? Your function will not be correct if formated is too small.
Code:
```void formatn(INT64 number, std::string& formated){
//...
};```
Regards,

Paul McKenzie

4. Member
Join Date
Feb 2009
Posts
252

## Re: Format Thousand Grouped Numbers

Originally Posted by Paul McKenzie
It doesn't work if the thousands seperator is the ",".

Use the system's locale to determine the thousands seperator, and not hard code the character. There are various Windows and C++ locale functions to determine this. Or at the very least, pass the thousands seperator as another parameter.

Secondly, why are you mixing 'C' and C++ style strings? Your function will not be correct if formated is too small.
Code:
```void formatn(INT64 number, std::string& formated){
//...
};```
Regards,

Paul McKenzie
I want it to be independant from regional settings.

I use c style strings cause I mostly work with char arrays.

5. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Format Thousand Grouped Numbers

I want it to be independant from regional settings.
So pass it as a parameter and do not hard code it into the function.
I use c style strings cause I mostly work with char arrays.
So why are you using char arrays instead of std::string? You're using std::string in the function.

Secondly, it still doesn't invalidate what I'm saying -- if formated is too small, that function will not work correctly as you will have a memory overwrite. You make no check to see if that char* actually has enough room. The user should pass to you the size of the buffer, so that you copy the right amount instead of overflowing.

Also, if I pass a string-literal to your function, it will also fail, probably crash:
Code:
```char *p = "abc12345674533";
formatN( 1000, p );  // this will crash.```
The bottom line is that your function is easily broken, and a properly written C++ function shouldn't be so fragile.

Regards,

Paul McKenzie
Last edited by Paul McKenzie; October 9th, 2010 at 06:10 PM.

6. Member
Join Date
Feb 2009
Posts
252

## Re: Format Thousand Grouped Numbers

Thx for your sugerencies, the buffer will be reserved specially to recive these numbers, also the values come directly from a Unsigned integer variable.

there is no user imput, just to report progress, i think there wont be problems, but im taking care about your sugerencies.

I always prefered chars arrays over strings cause i feel more control about and best performance working with char arrays since, i know exactly whats going on.

Instead im not 100% sure whats going on inside the strings class.

your comment still useful since i needed the way to pass a string by reference and i saw your comment.

std::string& formated =D

fixed my problem with another function im writing.

7. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Format Thousand Grouped Numbers

I always prefered chars arrays over strings cause i feel more control about and best performance working with char arrays since,
That is a myth that handling char arrays has "better performance" than just handling std::string.
i know exactly whats going on.
So why didn't you use char arrays in your function? Instead you used std::string to do the work. Secondly, every C++ programmer knows what is going on in an array -- that's not the point. The point is that using std::string makes the program safer and less error-prone. I already showed you how fragile your function is.
Instead im not 100&#37; sure whats going on inside the strings class.
You don't need to know what's going on inside. You use the public interface and everything works.

http://www.parashift.com/c++-faq-lite/containers.html
the buffer will be reserved specially to recive these numbers,
Famous last words. If a function has a hole, expect it to be opened up at some point, regardless of where the data may come from or how careful you think you're handling data and buffers. Again, C++ functions should never have to be written with such vulnerabilities.

Regards,

Paul McKenzie
Last edited by Paul McKenzie; October 10th, 2010 at 06:22 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•