CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    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.


    Thx in Advance.

  2. #2
    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. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Format Thousand Grouped Numbers

    Quote Originally Posted by Alphadan View Post
    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. #4
    Join Date
    Feb 2009
    Posts
    252

    Re: Format Thousand Grouped Numbers

    Quote Originally Posted by Paul McKenzie View Post
    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. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Format Thousand Grouped Numbers

    Quote Originally Posted by Alphadan View Post
    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. #6
    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.

    thx again for your time.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured