CString str; int num = 42; // set the string to "The answer is 42" str.Format("The answer is %d", num);
CString str; double x = 3.14159; // set the string to "The value of pi is 3.14" str.Format("The value of pi is %.2f", x);In this example, we create another CString object called str and a double variable called x. We use the CString Format function to format the string with the floating-point value with 2 decimal places and set the resulting string to the CString variable. Since CString Format is available as part of the Microsoft Foundation Classes (MFC) library, you will need to include the appropriate header file (afx.h) and link against the MFC library to use it in your C++ program.