-
December 4th, 2002, 04:33 PM
#1
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.
-
December 4th, 2002, 04:46 PM
#2
Look up sprintf or CString Format in MSDN.
-
December 4th, 2002, 05:38 PM
#3
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
-
December 4th, 2002, 08:44 PM
#4
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.
-
December 5th, 2002, 08:46 AM
#5
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
-
December 5th, 2002, 01:45 PM
#6
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);
}
-
December 5th, 2002, 02:02 PM
#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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|