m_hEdit.GetWindowText(strValue);
float x;
怎样把strValue转换成float类型的x?
请各位执教阿!
14 个解决方案
#1
strValue = "121110987654321";
dValue = atof(strValue);
dValue = atof(strValue);
#2
sprintf("%f",strValue);
#3
atof(将字符串转换成浮点型数)
相关函数 atoi,atol,strtod,strtol,strtoul
表头文件 #include <stdlib.h>
定义函数 double atof(const char *nptr);
函数说明 atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分,如123.456或123e-2。
返回值 返回转换后的浮点型数。
附加说明 atof()与使用strtod(nptr,(char**)NULL)结果相同。
范例 /* 将字符串a 与字符串b转换成数字后相加*/
#include<stdlib.h>
main()
{
char *a=”-100.23”;
char *b=”200e-2”;
float c;
c=atof(a)+atof(b);
printf(“c=%.2f\n”,c);
}
执行 c=-98.23
#4
double strtod(
const char *nptr,
char **endptr
);
double atof(
const char *string
);
const char *nptr,
char **endptr
);
double atof(
const char *string
);
#5
看错了
应该
d = atof(temp);
应该
d = atof(temp);
#6
double atof(
const char *string
);
int atoi(
const char *string
);
_int64 _atoi64(
const char *string
);
long atol(
const char *string
);
const char *string
);
int atoi(
const char *string
);
_int64 _atoi64(
const char *string
);
long atol(
const char *string
);
#7
atof即可
#8
atof
#9
atof
#10
#ifdef _UNICODE
int Str2Int(LPCTSTR str)
{
char chstr[100];
memset(chstr,0,sizeof(chstr));
WideCharToMultiByte(CP_ACP,0,str,wcslen(str),chstr,100,NULL,NULL);
return atoi(chstr);
}
float Str2Float(LPCTSTR str)
{
char chstr[100];
memset(chstr,0,sizeof(chstr));
WideCharToMultiByte(CP_ACP,0,str,wcslen(str),chstr,100,NULL,NULL);
return (float)atof(chstr);
}
#else
int Str2Int(LPCTSTR str)
{
return atoi(str);
}
float Str2Float(LPCTSTR str)
{
return (float)atof(chstr);
}
#endif
没什么长进,就是加了对 UNICODE的处理
int Str2Int(LPCTSTR str)
{
char chstr[100];
memset(chstr,0,sizeof(chstr));
WideCharToMultiByte(CP_ACP,0,str,wcslen(str),chstr,100,NULL,NULL);
return atoi(chstr);
}
float Str2Float(LPCTSTR str)
{
char chstr[100];
memset(chstr,0,sizeof(chstr));
WideCharToMultiByte(CP_ACP,0,str,wcslen(str),chstr,100,NULL,NULL);
return (float)atof(chstr);
}
#else
int Str2Int(LPCTSTR str)
{
return atoi(str);
}
float Str2Float(LPCTSTR str)
{
return (float)atof(chstr);
}
#endif
没什么长进,就是加了对 UNICODE的处理
#11
楼上各位都是把Char*转换成Double或者Float类型,并不是把CString转换成float类型阿
#12
CString::operator LPCTSTR
该成员函数会隐式转换CString对象到const char*
所以可以这么用:
CString strFloat("123.456");
double d = atof(strFloat); //double atof(const char *string);
该成员函数会隐式转换CString对象到const char*
所以可以这么用:
CString strFloat("123.456");
double d = atof(strFloat); //double atof(const char *string);
#13
1、CString可以隐式转换为LPCTSTR,而LPCTSTR就是const TCHAR *。
2、TCHAR会根据_UNICODE宏定义与否转义为wchar_t或者char。
3、用_tcstod,它是配合TCHAR使用的,也就是说,你不用考虑是不是unicode
2、TCHAR会根据_UNICODE宏定义与否转义为wchar_t或者char。
3、用_tcstod,它是配合TCHAR使用的,也就是说,你不用考虑是不是unicode
#14
/* ATOF.C: This program shows how numbers stored
* as strings can be converted to numeric values
* using the atof, atoi, and atol functions.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char *s; double x; int i; long l;
s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );
s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}
Output
atof test: ASCII string: -2309.12E-15 float: -2.309120e-012
atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
atoi test: ASCII string: -9885 pigs integer: -9885
atol test: ASCII string: 98854 dollars long: 98854
* as strings can be converted to numeric values
* using the atof, atoi, and atol functions.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char *s; double x; int i; long l;
s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );
s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}
Output
atof test: ASCII string: -2309.12E-15 float: -2.309120e-012
atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
atoi test: ASCII string: -9885 pigs integer: -9885
atol test: ASCII string: 98854 dollars long: 98854
#1
strValue = "121110987654321";
dValue = atof(strValue);
dValue = atof(strValue);
#2
sprintf("%f",strValue);
#3
atof(将字符串转换成浮点型数)
相关函数 atoi,atol,strtod,strtol,strtoul
表头文件 #include <stdlib.h>
定义函数 double atof(const char *nptr);
函数说明 atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分,如123.456或123e-2。
返回值 返回转换后的浮点型数。
附加说明 atof()与使用strtod(nptr,(char**)NULL)结果相同。
范例 /* 将字符串a 与字符串b转换成数字后相加*/
#include<stdlib.h>
main()
{
char *a=”-100.23”;
char *b=”200e-2”;
float c;
c=atof(a)+atof(b);
printf(“c=%.2f\n”,c);
}
执行 c=-98.23
#4
double strtod(
const char *nptr,
char **endptr
);
double atof(
const char *string
);
const char *nptr,
char **endptr
);
double atof(
const char *string
);
#5
看错了
应该
d = atof(temp);
应该
d = atof(temp);
#6
double atof(
const char *string
);
int atoi(
const char *string
);
_int64 _atoi64(
const char *string
);
long atol(
const char *string
);
const char *string
);
int atoi(
const char *string
);
_int64 _atoi64(
const char *string
);
long atol(
const char *string
);
#7
atof即可
#8
atof
#9
atof
#10
#ifdef _UNICODE
int Str2Int(LPCTSTR str)
{
char chstr[100];
memset(chstr,0,sizeof(chstr));
WideCharToMultiByte(CP_ACP,0,str,wcslen(str),chstr,100,NULL,NULL);
return atoi(chstr);
}
float Str2Float(LPCTSTR str)
{
char chstr[100];
memset(chstr,0,sizeof(chstr));
WideCharToMultiByte(CP_ACP,0,str,wcslen(str),chstr,100,NULL,NULL);
return (float)atof(chstr);
}
#else
int Str2Int(LPCTSTR str)
{
return atoi(str);
}
float Str2Float(LPCTSTR str)
{
return (float)atof(chstr);
}
#endif
没什么长进,就是加了对 UNICODE的处理
int Str2Int(LPCTSTR str)
{
char chstr[100];
memset(chstr,0,sizeof(chstr));
WideCharToMultiByte(CP_ACP,0,str,wcslen(str),chstr,100,NULL,NULL);
return atoi(chstr);
}
float Str2Float(LPCTSTR str)
{
char chstr[100];
memset(chstr,0,sizeof(chstr));
WideCharToMultiByte(CP_ACP,0,str,wcslen(str),chstr,100,NULL,NULL);
return (float)atof(chstr);
}
#else
int Str2Int(LPCTSTR str)
{
return atoi(str);
}
float Str2Float(LPCTSTR str)
{
return (float)atof(chstr);
}
#endif
没什么长进,就是加了对 UNICODE的处理
#11
楼上各位都是把Char*转换成Double或者Float类型,并不是把CString转换成float类型阿
#12
CString::operator LPCTSTR
该成员函数会隐式转换CString对象到const char*
所以可以这么用:
CString strFloat("123.456");
double d = atof(strFloat); //double atof(const char *string);
该成员函数会隐式转换CString对象到const char*
所以可以这么用:
CString strFloat("123.456");
double d = atof(strFloat); //double atof(const char *string);
#13
1、CString可以隐式转换为LPCTSTR,而LPCTSTR就是const TCHAR *。
2、TCHAR会根据_UNICODE宏定义与否转义为wchar_t或者char。
3、用_tcstod,它是配合TCHAR使用的,也就是说,你不用考虑是不是unicode
2、TCHAR会根据_UNICODE宏定义与否转义为wchar_t或者char。
3、用_tcstod,它是配合TCHAR使用的,也就是说,你不用考虑是不是unicode
#14
/* ATOF.C: This program shows how numbers stored
* as strings can be converted to numeric values
* using the atof, atoi, and atol functions.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char *s; double x; int i; long l;
s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );
s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}
Output
atof test: ASCII string: -2309.12E-15 float: -2.309120e-012
atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
atoi test: ASCII string: -9885 pigs integer: -9885
atol test: ASCII string: 98854 dollars long: 98854
* as strings can be converted to numeric values
* using the atof, atoi, and atol functions.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char *s; double x; int i; long l;
s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );
s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}
Output
atof test: ASCII string: -2309.12E-15 float: -2.309120e-012
atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
atoi test: ASCII string: -9885 pigs integer: -9885
atol test: ASCII string: 98854 dollars long: 98854