开发者

Convert CString to float in mfc

how can I convert a CString variable to a floating point? (I'm using visuall c++ 6.0 and the MFC)

I'm trying to use an edit box to return a value which I'm putting into an array of floating points. I'm Using the GetWindowText method to get the value, which returns a CString. So I need to convert to a floating point. (or am I just doing things completely the wrong way?).

I presume there are methods for doing this already in the MFC.(have already used the Format method to convet to a CString di开发者_JAVA百科splay the values in the array in the edit box)

Thanks.


you can just do

    CString pi = "3.14";
    return atof(pi);

EDIT

Also use this function:

    CString pi = "3.14";
    return _ttof(pi);

Reading a string value and parse/convert it to float allows you to locate the error when there is one. All you need is a help of a C Run-time function: strtod() or atof().

I would prefer strtod as the second argument returns a pointer to the string where the parse terminated:

 CString str;
m_edtMyEditBox.GetWindowText(str);
char *pEnd;
double dValue = strtod(str.GetBuffer(str.GetLength()), &pEnd);
if (*pEnd != '\0')
{
    // Error in parsing
}
str.ReleaseBuffer();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜