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
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
float f = 1.234;
double df = 1.234;
sprintf( str , "%.3f , %.3lf" , f , df );
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);
对于double,其对应的格式化字符是%lf
如
CString str;
str.Format("%.3lf",a);
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