float a = 345.123;
CString str;
str.Format("%.3f", a);  // 用法类似于C中的printf举例
CString str;str.Format(_T("Floating point: %.2f\n"), 12345.12345);
_tprintf("%s", (LPCTSTR) str);str.Format(_T("Left-justified integer: %.6d\n"), 35);
_tprintf("%s", (LPCTSTR) str);str.Format(IDS_SCORE, 5, 3);
_tprintf("%s", (LPCTSTR) str);
   
输出为:
Floating point: 12345.12
Left-justified integer: 000035
Penguins: 5
Flyers  : 3

解决方案 »

  1.   

    转换成char * 使用sprintfchar str[80];
    float f = 1.234;
    double df = 1.234;
    sprintf( str , "%.3f , %.3lf" , f , df );
      

  2.   

    另外提供一个常用的字符串格式化函数
    int sprintf( char *buffer, const char *format [, argument] ... );它的用法也是跟C中的printf差不多...比如:
    float a = 345.123;
    char str[10];
    sprintf(str, "%.3f", a);  // 用法类似于C中的printf
    ...另外还有一个函数喔.
    _ultoa();
    例如:
    float a = 345.123;
    char str[10];
    _ultoa(a, str, 10);
      

  3.   

    如果我要把double或float赋给CString(在CString变量中保留三位小数)呢?
      

  4.   

    更正上面一些同志的错误:
    对于double,其对应的格式化字符是%lf

    CString str;
    str.Format("%.3lf",a);
      

  5.   

    是否double或float 类型的变量不能负值给CString类型的变量呢?
      

  6.   

    ////Converts a floating-point number to a string.
    char *_fcvt(double value/*要转换的数*/,int count/*小数点后的位数*/,int *dec/*指向存放小数点的指针*/,int *sign/*指向符号标志指针*/);
    Example/* FCVT.C: This program converts the constant
     * 3.1415926535 to a string and sets the pointer
     * *buffer to point to that string.
     */#include <stdlib.h>
    #include <stdio.h>void main( void )
    {
       int  decimal, sign;
       char *buffer;
       double source = 3.1415926535;   buffer = _fcvt( source, 7, &decimal, &sign );
       printf( "source: %2.10f   buffer: '%s'   decimal: %d   sign: %d\n",
                source, buffer, decimal, sign );
    }
    Outputsource: 3.1415926535   buffer: '31415927'   decimal: 1   sign: 0 /////Converts a double number to a string.
    char *_evct(double value,int count,int *dec,int *sign);Example/* ECVT.C: This program uses _ecvt to convert a
     * floating-point number to a character string.
     */#include <stdlib.h>
    #include <stdio.h>void main( void )
    {
       int     decimal,   sign;
       char    *buffer;
       int     precision = 10;
       double  source = 3.1415926535;   buffer = _ecvt( source, precision, &decimal, &sign );
       printf( "source: %2.10f   buffer: '%s'  decimal: %d  sign: %d\n",
               source, buffer, decimal, sign );
    }
    Outputsource: 3.1415926535   buffer: '3141592654'  decimal: 1   sign: 0
      

  7.   

    是否double或float 类型的变量不能负值给CString类型的变量呢? 
      

  8.   

    ~{TZ~}msdn~{IO2i~}CString::Format~{:/J}#,TZ2iURO`9X5DDZH]#,;y1>IOHg:NW*;/6<;a:\Ge3~!#~}