|
CString a("2e3");<br />
double b = atof(a);
do right in VC++6.0 but wrong in VC++2008
CString a("2e3");<br />
double b = _tstof(a);
do right in VC++2008 but wrong in VC++6.0
|
|
|
|
|
It all depends if either _UNICODE or _MBCS are defined. Please check out MSDN for more information on this topic.
Regards,
Mihai Moga
|
|
|
|
|
Thats the TCHAR mapping for atof/_wtof. By default vc2008 defines TCHAR as a native type. You can use it in vc6, you just need to do so explicitly.
|
|
|
|
|
I am trying to figure out how to convert text in a CString to a double. I found this tutorial:
http://www.codeproject.com/KB/string/cstringmgmt.aspx#Converting%20a%20CString%20to%20an%20integer
and it contains this example code for converting to an integer
CString hex = _T("FAB");
CString decimal = _T("4011");
ASSERT(_tcstoul(hex, 0, 16) == _ttoi(decimal));
I don’t even see an integer declared in that fragment, much less how to convert the CString to an integer or decimal.
Here is what I need:
CString source = _T("1.2e6");
double target;
target = source.FormatAsDouble();
Obviously that does not work. But what will?
Thanks for your time
|
|
|
|
|
Rats, I miss typed and the edit did not seem to work.
This is what I want to do:
CString source = _T("1.2e6");
double target;
target = source.FormatAsDouble();
Thanks for your time
|
|
|
|
|
Hi, you should try like this:
CString source = _T("1.2e6");
double target = _tstof( source );
Regards,
Mihai Moga
|
|
|
|
|
I just compiled and ran that code, it works. But you already knew that.
Thank you Mihai.
The string to be converted will be extracted from a user dialog. The user may make an error in entering the number. Is there a method that will validate the string as being properly formatted or in some other way detect an input error to I can tell the user?
Thanks again.
Thanks for your time
|
|
|
|
|
Hi, for this task you should check out Ben Hanson's CFilterEdit[^] class. I use in one of my projects and I am gratefull to the author.
Regards,
Mihai Moga
|
|
|
|
|
RE: CFileterEDit, That class does look good. I downloaded it and the demo.
The file BaseEdit.h contains this reference:
#include <boost/regex.hpp>
This include file in not contained in either of the downloads. BaseEDit.h is the only file containing this include and it has no previous includes that lead me to the file.
Where do I find this file?
Thanks for your time
|
|
|
|
|
In order to recompile that project, you should consider downloading the boost::regex library from Boost's Official Page/[^].
Regards,
Mihai Moga
|
|
|
|
|
Hello Mahai,
I have installed the Boost environment, I have added FloatEdit and BaseEdit to my project, and they will compile. Now I see method
double CFloatEdit::GetValue () const
in file FloatEdit.cpp
But I don't see how to apply it to my dialog box.
One of the fields that I want filtered has the ID:
IDC_X_POSITION_EDIT
and string in the box can be accessed via the variable name as follows:
GBE_x_position_string.GetWindowTextW( str );
That puts the value into CString names str.
Can you tell me how to use this FloatEdit to check the inputs to this field?
Thanks for your time
|
|
|
|
|
Well, FloatEdit will do it for you, it filters data so that you would have a valid floating value/string. It's enought to subclass you CEdit control with this one.
Regards,
Mihai Moga
|
|
|
|
|
Thanks for your reply, but while the documentation tells that than I can use this class to filter the data, I am not able to see how to apply the class to do that. How do I use it?
Given that the text edit tool in my dialog has the id of
IDC_X_POSITION_EDIT
and the assigned variable of
GBE_x_position_string
with example usage: GBE_x_position_string.GetWindowTextW( str );
and variable str containg the string from said edit text tool:
Will you show me the line of code that invokes a method of FloatEdit to
1. validate the string as suitable for converting to float or double
and/or
2. provide the foat or double equvalent.
Thanks for your time
|
|
|
|
|
Hint: atof returns 0.0 whenever the conversion fails.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
But what if the value entered is 0.0. That may well be a valid entry. I could compare the source string with "0.0" to account for that one case, but that just seems ugly.
Thanks for your time
|
|
|
|
|
No it's NOT ugly and it is the way my suggestion pointed to. You have to deal only with 1 case and that's much more simpler than the general validation. Anyway you have to accomplish carefully your task. For instance 0 is a valid entry too (and, depending on your level of user-noise tolerance, also 0.00 , 0.000 ,... are).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hello Pallini,
I really do appreciate the time you spent answering my question. But we do disagree on something. Elegant is something like this.
CString source = "1.23e6";
double target;
int status;
status = source.ConvertToFloat( &target );
If atof returns 0.0 in the double variable for an error, this it must also write 0.0 to the variable when the text is 0, or 0.0, or any of several other conditions as you have noted. In this event, either before the convert or after, we must have additonal code to check for any text string that would result in 0.0 to determine if we have an error, or if we really have a 0. Having to writing extra code to do that is indeed downright ugly.
Maybe I have missunderstood you. If so please help me out.
Thanks for your time
|
|
|
|
|
No, you haven't misunderstood me.
I mean atof , returning 0 on error, gives you the support to implement the elegant solution you proposed: it's a starting point.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Unfair vote[^]
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Already balanced, sir.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thank you [Military tone] Sirrrr! [/Military tone]
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
You can also use the functions isalfa () or isdigit ().
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Hello Nelek,
I had forgotton about those. But then I would need to write some code that checks the string one char at a time.
I think that the best way is to use the CFilterEdit. But that requires getting and installing the Boost package mentioned elsewhere. It does seem to be far better than any than any thing else I have see.
But think about this for a minute. All user interface with dialogs involve the user typing in a string and the programmer converting at needed. The interface between the dialog and the remainder of the program is a string, period. The very idea that any programmer needs to go to this amount of effort just to get a string ( CString ) converted to an integer, float, double or what have you, is just down right absurd.
Am I the only one that recognizes this. If so, maybe I am the crazy one. Let me know.
Meanwhile, following Nelek's note in his/her signature:
I am grateful for each and every one of you that responded. Maybe this will be documented here or somewhere so others can find it.
Indeed, which I get this straight, I will write it up and add it to my site here:
www.bkelly.ws/software
I am very much open to suggestions and if you have a page written, put your name in there showing credit to you and I will be happy to post it and give you credit.
But first, I want to get my program working.
Thank you.
Thanks for your time
|
|
|
|
|
bkelly13 wrote: in his/her signature
His :P
Actually the MFC let you set a member variable for a CEdit as number typ and it gives the error message when you are giving letters by itself. Just put the member variable as double insteads of CEdit*
Problem... when using APIs this can't be used.
But there is another way to do it, with the DDX or DDV (don't remember it well). I read in a manual that the checking can be automated using one of them and you can use the same validation for more than one control. But not sure about it because I was lazy and I used the other option (member variable as the type of number I needed)
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Have you considered a masked-edit control? Several exist here on CP's site.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|