CString format and numbers
-
Is there a straight forward way to make a number like 123 and turn it into a string with left padded zeros. Example "123" into 0000123 A way that does not require a silly loop like this. CString strSecId; strSecId.Format("%d", m_iSectionId); while(strSecId.GetLength() < 7) strSecId = "0" + strSecId; //left pad with zeros thanks
-
Is there a straight forward way to make a number like 123 and turn it into a string with left padded zeros. Example "123" into 0000123 A way that does not require a silly loop like this. CString strSecId; strSecId.Format("%d", m_iSectionId); while(strSecId.GetLength() < 7) strSecId = "0" + strSecId; //left pad with zeros thanks
Try "%.7d". Don't try it, just do it! ;-)
-
Is there a straight forward way to make a number like 123 and turn it into a string with left padded zeros. Example "123" into 0000123 A way that does not require a silly loop like this. CString strSecId; strSecId.Format("%d", m_iSectionId); while(strSecId.GetLength() < 7) strSecId = "0" + strSecId; //left pad with zeros thanks
Your code looks great until that last line. Try this:
strSecId.Insert(0, "0");
The first argument is the 0-based index of where you want to insert this string (we want it at the fron, hence the 0) and the second argument is the string to insert (a 0!) Hope this helps! Danny -
Is there a straight forward way to make a number like 123 and turn it into a string with left padded zeros. Example "123" into 0000123 A way that does not require a silly loop like this. CString strSecId; strSecId.Format("%d", m_iSectionId); while(strSecId.GetLength() < 7) strSecId = "0" + strSecId; //left pad with zeros thanks
Use:
strSecId.Format("%07d", m_iSectionId);
-
Use:
strSecId.Format("%07d", m_iSectionId);