举例说明CString类的Format(...)的用法。谢谢,放分

解决方案 »

  1.   

    CString str;
    str.Format("%d", 1); // str == "1"
    其余格式控制符参照printf
      

  2.   

    CString str;
    str.Format("%d", 1);
      

  3.   

    CString str;
    int a;
    str.Format("%d", a);
      

  4.   

    CString str = "Some Data";
    str.Format(_T("%s%d"), str, 123) );  str.Format(_T("Floating point: %.2f\n"), 12345.12345);str.Format(_T("Left-justified integer: %.6d\n"), 35);str.Format(IDS_SCORE, 5, 3);
      

  5.   

    和printf类似
    http://www.vcshare.net/c/
      

  6.   

    CString str;
    char ch;
    str.Format("%c", ch);
      

  7.   

    CString errormessage;
    errormessage.Format("連接數據庫失敗!\r\n錯誤信息:%s",e.ErrorMessage());
      

  8.   

    int x = 123;
    CString str;
    str.Format("%d", x);         // 123
    str.Format("%6d", x);        //   123
    str.Format("%06d", x);       //000123
    str.Format("%x", x);         //7b
    str.Format("%X", x);         //7B
    str.Format("%04x", x);       //007bfloat x = 123.456;
    str.Format("%f", x);         // 123.456
    str.Format("%2.3f");         // 123.456
    str.Format("%4.2f");         //  123.45
      

  9.   

    记住, 和printf()中的大体一致
      

  10.   

    int    i = 10;
       double fp = 1.5;
       CString  str;
       char   c = '\n';   str.Format(  "你好%c", c );  //str的值  你好\n
       str.Format(  "%d", i );
       str.Format(  "%f", fp );
      

  11.   

    来晚了,查查msdn就全知道了。
      

  12.   

    顺路问一句,我所说的跟上面所提到的直接将 int 向转化有些不同。在一个CString 变量中存放的是数字,如果想依据这个数字的长度来确定前面是否加零。比如说数字是 3 则转化为 03 ,如果是16 则原样存为 16.
    请教该如何实现。
      

  13.   

    str.Format("%02d", i);  //2指示长度,0指不够长时在前加"0"
    //3 -> "03"
    //16 -> "16
      

  14.   

    CString str;str.Format(_T("Floating point: %.2f\n"), 12345.12345);
    _tprintf(_T("%s"), (LPCTSTR) str);str.Format(_T("Left-justified integer: %.6d\n"), 35);
    _tprintf(_T("%s"), (LPCTSTR) str);str.Format(IDS_SCORE, 5, 3);
    _tprintf(_T("%s"), (LPCTSTR) str);
      

  15.   

    CString str;str.Format(_T("Floating point: %.2f\n"), 12345.12345);
    _tprintf(_T("%s"), (LPCTSTR) str);str.Format(_T("Left-justified integer: %.6d\n"), 35);
    _tprintf(_T("%s"), (LPCTSTR) str);str.Format(IDS_SCORE, 5, 3);
    _tprintf(_T("%s"), (LPCTSTR) str);