CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jun 2003
    Posts
    188

    Formatting number strings?

    I have a number as a string

    string aaa;
    //assign string number
    aaa = "333333333";

    And I would like to reformat the number to make it more readable...
    //reformat
    aaa = "333,333,333"

    Is there an easy way to do this?

  2. #2
    Join Date
    Jan 2001
    Location
    The Netherlands
    Posts
    100
    Maybe something like this?
    PHP Code:
    void MakeNiceString(CString *nStr,int nSpace,CString nChar)
    {
        
    int y nStr->GetLength();    
        
    int iCharCount 0;

        while (
    0
        {
            if(
    iCharCount == nSpace)
            {
                
    iCharCount 0;
                
    nStr->Insert(y,nChar);
            }

            
    iCharCount++;
            
    y--;
        }
    }

    CString aaa "3333333333";
    MakeNiceString(&aaa,3,",");
    AfxTrace(aaa); // 3,333,333,333 
    /* Regards, David Smulders */

  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Just wondering if there is any C++ way where we can use std::locale and std::facet to achieve the same thing. Unfortunately, so far my google search has not find any good example.

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Okay, just manage to get this code to work on VC++.Net. However, on VC++6, dvalue is not being displayed properly. Guess that is because VC++6 is not as standard complaint as compared to VC++.Net.

    Code:
    #include <locale>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int ivalue = 10000;
        double dvalue = 20000.202;
    
        cout.imbue( locale("enu") );
        cout << ivalue << endl;
        cout << dvalue << endl;
    
        cout.imbue( locale::classic() );  // Restore to classic C locale.
    
        return 0;
    }
    Last edited by Kheun; July 29th, 2004 at 03:09 AM.

  5. #5
    Join Date
    Jul 2004
    Location
    Lahore
    Posts
    27

    Here is a function for VC++ 6.0

    This will insert commas on exact locations

    void FormatNumber(CString& vol)
    {
    vol.TrimLeft();
    vol.TrimRight();

    int len = vol.GetLength();


    for(int i = len - 3 ; i > 0 ; i -= 3)
    {
    vol.Insert(i,",");

    }
    }

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773
    will these people please STOP offering solutions using MFC! This is a non-MFC forum and the original poster did not specify MFC.

    Now, how to insert a comma every 3rd character from the right? (I like Kheun's idea but we know we have started with a string and we'll assume we know it's numeric and want a comma and not any other symbol).

    Now let's go for an old-fashioned for-loop:

    inserting from the front...
    Code:
    std::string s;
    len = s.length();
    for ( int i = ((len-1)%3) + 1; i < len; i+=4, ++len )
    {
      s.insert( i, 1, ',' );
    }
    from the back I think this will work:
    Code:
    std::string s;
    len = s.length();
    for ( int i= len - 3; i > 0; i -= 3 )
    {
      s.insert( i, 1, ',' );
    }
    Anyone who can come up with wonderful insert_iterator and transform or whatever to do something like this will get prizes for elegance.
    Last edited by NMTop40; July 29th, 2004 at 06:23 AM.

  7. #7
    Join Date
    Jun 2003
    Posts
    188
    Thanks for all the help, guys.

  8. #8
    Join Date
    Jun 2003
    Posts
    188
    Well, there is a new wrinkle in this. I was not told this, the numbers are dollar amounts with cents.

    so 100000.00 needs to look like 100,000.00

  9. #9
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    Quote Originally Posted by Kheun
    Code:
    #include <locale>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int ivalue = 10000;
        double dvalue = 20000.202;
    
        cout.imbue( locale("enu") );
        cout << ivalue << endl;
        cout << dvalue << endl;
    
        cout.imbue( locale::classic() );  // Restore to classic C locale.
    
        return 0;
    }
    You use a hardcoded "enu" and you restore to ::classic which might be wrong.
    Better:
    Code:
    #include <iostream>
    #include <locale>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
      locale loc("");
      locale loc_previous = cout.imbue(loc);
      cout.precision(2);
      cout << std::fixed;
      double d = 2002.256530;
      cout << d << std::endl;
      cout.imbue(loc_previous);
      cout << d << std::endl;
    
      return 0;
    }
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  10. #10
    Join Date
    Jun 2003
    Posts
    188
    Marc, I need to toss the formatted number into a string. It is going to be later written out to a file.

  11. #11
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    Then use a stringstream.
    Code:
      stringstream ss;
      locale loc("");
      locale loc_previous = ss.imbue(loc);
      ss.precision(2);
      ss << std::fixed;
      double d = 2002.256530;
      ss << d << std::endl;
      ss.imbue(loc_previous);
    
      // Get the string from the stringstream:
      ss.str();
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  12. #12
    Join Date
    Jun 2003
    Posts
    188
    I am having trouble using streams in this project. I get linker errors in libcpd.lib. The C++ streams have a conflict with the MySql lib. Is there another solution?

  13. #13
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Could you post some of the errors?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  14. #14
    Join Date
    Jun 2003
    Posts
    188
    To link in mysqlclient.lib, I had to tell the linker to ignore LIBCD.lib or I get link redef errors.

    So if I put this in

    ifstream a("lala.txt");

    Here are the link errors...

    ibcpd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __malloc_dbg referenced in function "void * __cdecl operator new(unsigned int,struct std::_DebugHeapTag_t const &,char *,int)" (??2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z)
    libcpd.lib(locale.obj) : error LNK2001: unresolved external symbol __malloc_dbg
    libcpd.lib(_tolower.obj) : error LNK2001: unresolved external symbol __malloc_dbg
    libcpd.lib(xwcsxfrm.obj) : error LNK2019: unresolved external symbol __malloc_dbg referenced in function __Wcsxfrm
    libcpd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __free_dbg referenced in function "void __cdecl operator delete(void *,struct std::_DebugHeapTag_t const &,char *,int)" (??3@YAXPAXABU_DebugHeapTag_t@std@@PADH@Z)
    libcpd.lib(xwcsxfrm.obj) : error LNK2001: unresolved external symbol __free_dbg
    libcpd.lib(locale.obj) : error LNK2019: unresolved external symbol __realloc_dbg referenced in function "private: void __thiscall std::locale::_Locimp::_Addfac(class std::locale::facet *,unsigned int)" (?_Addfac@_Locimp@locale@std@@AAEXPAVfacet@23@I@Z)
    libcpd.lib(xmbtowc.obj) : error LNK2001: unresolved external symbol _errno
    libcpd.lib(xwcscoll.obj) : error LNK2001: unresolved external symbol _errno
    libcpd.lib(xstrcoll.obj) : error LNK2001: unresolved external symbol _errno
    libcpd.lib(locale.obj) : error LNK2001: unresolved external symbol _errno
    libcpd.lib(xwctomb.obj) : error LNK2001: unresolved external symbol _errno
    libcpd.lib(wlocale.obj) : error LNK2001: unresolved external symbol _errno
    libcpd.lib(xlocale.obj) : error LNK2001: unresolved external symbol _errno
    libcpd.lib(_tolower.obj) : error LNK2019: unresolved external symbol __chvalidator referenced in function __Tolower
    libcpd.lib(_toupper.obj) : error LNK2019: unresolved external symbol __chvalidator referenced in function __Toupper
    libcpd.lib(xmbtowc.obj) : error LNK2019: unresolved external symbol __CrtDbgReport referenced in function __Mbrtowc
    Debug/EasyCheckup.exe : fatal error LNK1120: 6 unresolved externals

    Build log was saved at "file://c:\EZTax Projects\EasyCheckup\Debug\BuildLog.htm"
    EasyCheckup - 18 error(s), 3

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