Format string with commas and decimal
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Format string with commas and decimal

  1. #1
    Join Date
    Aug 2002
    Posts
    11

    Question Format string with commas and decimal

    I have a string of numbers that I need format with commas and decimal places with 2 point precision (i.e. 1,000,000.00). Can someone please explain how I would go about accomplishing this?

    I am using VC++ 6.0 on a Windows 2000 platform.

  2. #2
    Join Date
    Aug 2001
    Location
    North Bend, WA
    Posts
    1,947
    Look up sprintf or CString Format in MSDN.

  3. #3
    Join Date
    Oct 2002
    Posts
    1,134
    I suppose something along the lines of:
    Code:
    int number = value; // "value" is floating point
    millions = number / 1000000;
    thousands = ( number - millions * 1000000 ) / 1000;
    units = number - ((millions * 1000 + thousands) * 1000;
    decimal = (int)((value - number) * 100);
    sprintf( "%03d,%03d,%03d.%02d", millions, thousands, hundreds,
    units, decimal );
    You'll have to handle the case of millions being zero, etc.
    Regards
    Robert Thompson

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,553
    I think the original question mentioned that he had a "string" of numbers.

    In that case, maybe he should use the _atoi() function, to change the string value to numeric, after which the sprintf formats should work quite nicely.

  5. #5
    Join Date
    Oct 2002
    Posts
    1,134
    I suppose it depends on one's interpretation of "string". I was thinking of a string of characters, but you have a point that it may well have been a sequence of numbers. We need a clarification, Joseph!
    Regards
    Robert Thompson

  6. #6
    Join Date
    Aug 2002
    Posts
    11

    Formatting a String

    Basically, I have a label that I want to populate from a numerical keypad I have on screen. The system we are designing uses touch screens. When anumber, 0 - 9, is pressed the value associated with the keypad is appended to my Keypad string.

    What I want to happen is when the numerical sequence being entered becomes greater than 999, 999,000, etc, I want to add the commas. My current code looks like the following. m_Keypad String is CString.

    void CConfigRngTrkrDlg::OnKeypad0btn()
    {
    m_KeypadEntry = "0";
    SetConfigLbl(m_KeypadEntry);

    }

    void CConfigRngTrkrDlg::SetConfigLbl(CString m_KeypadEntry)
    {
    m_KeypadString += m_KeypadEntry;
    m_TxFreqLbl.SetFontSize(14);
    m_TxFreqLbl.SetFontBold(TRUE);
    m_TxFreqLbl.SetTextColor(RGB(0,0,0));
    m_TxFreqLbl.SetText(m_KeypadString);

    }

  7. #7
    What I want to happen is when the numerical sequence being entered becomes greater than 999, 999,000, etc, I want to add the commas. My current code looks like the following. m_Keypad String is CString.

    How about this?

    //m_nCount is an int, either a static or in scope for the whole
    //class, that gets incremented every time this action occurs, then
    // reset to 0 when it gets to 3...

    void CConfigRngTrkrDlg::SetConfigLbl(CString& m_KeypadEntry)
    {
    m_KeyPadString += m_KeyPadEntry;

    m_nCount++;

    if(m_nCount == 3)
    {
    m_KeyPadString.InsertAt(m_KeyPadString.GetLenght() - 3, ",");

    m_nCount = 0; // reset it to 0, so it will wait and enter 3 more...

    }


    void );

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center