首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

vc中怎么将double转化成string型,不是format的那个

2012-01-15 
vc中如何将double转化成string型,不是format的那个vc中如何将double转化成string型,不是format的那个,form

vc中如何将double转化成string型,不是format的那个
vc中如何将double转化成string型,不是format的那个,format的转换后会自动定义格式,如3.2转化后默认的是3.200000,如果不用定义保留的位数,就是直接转换的应该是哪个方法,最好要有个例子的,多谢!

[解决办法]
在double类型的变量前加(_bstr_t)强制转换.下面例子,已调试

double xxx = 1.2312;
_bstr_t yyy = (_bstr_t)xxx;
MessageBox(yyy);


[解决办法]
用sprintf( char *buffer, const char *format [, argument] ... );
#include <stdio.h>

void main( void )
{
char buffer[200], s[] = "computer ", c = 'l ';
int i = 35, j;
float fp = 1.7320534f;

/* Format and print various data: */
j = sprintf( buffer, "\tString: %s\n ", s );
j += sprintf( buffer + j, "\tCharacter: %c\n ", c );
j += sprintf( buffer + j, "\tInteger: %d\n ", i );
j += sprintf( buffer + j, "\tReal: %f\n ", fp );

printf( "Output:\n%s\ncharacter count = %d\n ", buffer, j );
}


Output

Output:
String: computer
Character: l
Integer: 35
Real: 1.732053

character count = 71


[解决办法]
Converts a double number to a string. This is a version of _ecvt with security enhancements as described in Security Enhancements in the CRT.


errno_t _ecvt_s(
char * _Buffer,
size_t _SizeInBytes,
double _Value,
int _Count,
int *_Dec,
int *_Sign
);
template <size_t size>
errno_t _ecvt_s(
char (&_Buffer)[size],
double _Value,
int _Count,
int *_Dec,
int *_Sign
); // C++ only



Parameters
[out] _Buffer
Filled with the pointer to the string of digits, the result of the conversion.

[in] _SizeInBytes
Size of the buffer in bytes.

[in] _Value
Number to be converted.

[in] _Count
Number of digits stored.

[out] _Dec
Stored decimal-point position.

[out] _Sign
Sign of the converted number.

Return Value
Zero if successful. The return value is an error code if there is a failure. Error codes are defined in Errno.h. For more information, see errno, _doserrno, _sys_errlist, and _sys_nerr.

In the case of an invalid parameter, as listed in the following table, this function invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, this function sets errno to EINVAL and returns EINVAL.

[解决办法]
//Example

// ecvt_s.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main( )
{
char * buf = 0;
int decimal;
int sign;
int err;

buf = (char*) malloc(_CVTBUFSIZE);
err = _ecvt_s(buf, _CVTBUFSIZE, 1.2, 5, &decimal, &sign);

if (err != 0)
{
printf( "_ecvt_s failed with error code %d\n ", err);
exit(1);
}

printf( "Converted value: %s\n ", buf);

}

热点排行