You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
You really like complication . If the goal is just to process real numbers, use StringToDouble() and a processing of special case 0.
That was the initial solution:
If you don't expect the value to ever be 0, type-cast the string to a double and test that it isn't equal to 0.
However, the OP wanted to be able to deal with distinguishing whether 0 was the entered value, or if 0 was the result of the string being an invalid number. Both "0" and "sfdlgkjsflkjdsklfsd" are going to result in 0 with StringToDouble() or (double).
This was unacceptable to the OP which then spawned this subsequent discussion.
That was the initial solution:
However, the OP wanted to be able to deal with distinguishing whether 0 was the entered value, or if 0 was the result of the string being an invalid number. Both "0" and "sfdlgkjsflkjdsklfsd" are going to result in 0 with StringToDouble() or (double).
This was unacceptable to the OP which then spawned this subsequent discussion.
Simplified example :
if(value==0)
{
if(inputs=="0" || inputs=="0.0")
{
//--- all is ok
}
else
{
//--- wrong inputs
}
}
That's why I said "and a processing of special case 0."
Simplified example :
if(value==0)
{
if(inputs=="0" || inputs=="0.0")
{
//--- all is ok
}
else
{
//--- wrong inputs
}
}
And what about 0.00?
Or +0.00?
Or .0?
Yep, that is a problem.
You could do a string comparison if the cast value = 0
i.e. if(cast_value == 0 && str_value == "0")
But you would have to think about 0.0 or 0.00 being entered.
You could burst the string into a character array and test each character.
Depends how important this is.
And what about 0.00?
Or +0.00?
Or .0?
That's why I said "Simplified example". I will not do the job for the OP.
It is an interesting exercise, nevertheless i.e. is it better to have a list of possible variations of "0", or just test everything?
The former is arguably quicker but at the risk of missing a legitimate variation.
However, how important is speed when it should only be tested on a CHARTEVENT_OBJECT_ENDEDIT?
Regardless, I'm sure the OP now has plenty to go on!
It is an interesting exercise, nevertheless i.e. is it better to have a list of possible variations of "0", or just test everything?
The former is arguably quicker but at the risk of missing a legitimate variation.
However, how important is speed when it should only be tested on a CHARTEVENT_OBJECT_ENDEDIT?
Regardless, I'm sure the OP now has plenty to go on!
Or using regular expression :-D
You could also ask to input zero as "0" and reject all other cases.
Or using regular expression :-D
Personally, I would be taking an approach like this:
Once ENDEDIT happens, cast the OBJ_TEXT into a double and then push it back into the edit box as a string (using StringFormat() if I wanted to control the format). User will immediately see the outcome of their actions and either change it if it doesn't suit their needs, or leave it.
e.g.
#define EDIT_BOX "EditBox"
int OnInit()
{
ObjectCreate (0, EDIT_BOX, OBJ_EDIT, 0, 0, 0);
ObjectSetInteger(0, EDIT_BOX, OBJPROP_XDISTANCE, 200);
ObjectSetInteger(0, EDIT_BOX, OBJPROP_YDISTANCE, 200);
ObjectSetInteger(0, EDIT_BOX, OBJPROP_XSIZE, 100);
ObjectSetInteger(0, EDIT_BOX, OBJPROP_YSIZE, 20);
ObjectSetInteger(0, EDIT_BOX, OBJPROP_ALIGN, ALIGN_CENTER);
ObjectSetString (0, EDIT_BOX, OBJPROP_TEXT, "Enter Value");
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
ObjectDelete(0, EDIT_BOX);
}
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
{
if(id==CHARTEVENT_OBJECT_ENDEDIT && sparam==EDIT_BOX)
{
double value = (double) ObjectGetString(0, EDIT_BOX, OBJPROP_TEXT);
ObjectSetString(0, EDIT_BOX, OBJPROP_TEXT, (string)value);
}
}
Personally, I would be taking an approach like this:
Once ENDEDIT happens, cast the OBJ_TEXT into a double and then push it back into the edit box as a string (using StringFormat() if I wanted to control the format). User will immediately see the outcome of their actions and either change it if it doesn't suit their needs, or leave it.
e.g.
#define EDIT_BOX "EditBox"
int OnInit()
{
ObjectCreate (0, EDIT_BOX, OBJ_EDIT, 0, 0, 0);
ObjectSetInteger(0, EDIT_BOX, OBJPROP_XDISTANCE, 200);
ObjectSetInteger(0, EDIT_BOX, OBJPROP_YDISTANCE, 200);
ObjectSetInteger(0, EDIT_BOX, OBJPROP_XSIZE, 100);
ObjectSetInteger(0, EDIT_BOX, OBJPROP_YSIZE, 20);
ObjectSetInteger(0, EDIT_BOX, OBJPROP_ALIGN, ALIGN_CENTER);
ObjectSetString (0, EDIT_BOX, OBJPROP_TEXT, "Enter Value");
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
ObjectDelete(0, EDIT_BOX);
}
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)
{
if(id==CHARTEVENT_OBJECT_ENDEDIT && sparam==EDIT_BOX)
{
double value = (double) ObjectGetString(0, EDIT_BOX, OBJPROP_TEXT);
ObjectSetString(0, EDIT_BOX, OBJPROP_TEXT, (string)value);
}
}
You will need a way to validate your input. (an additional button ?)
Keep it simple. Anyway, as you wish :p
You will need a way to validate your input. (an additional button ?)
I'm not sure I follow...?
I may have misunderstood the OP's intention, but I believe the interface will be dynamic i.e. a change in this edit box will result in an update of another box accordingly.
You could have a validation button, but if the result is simply displayed (rather than actioned) it may be an unnecessary addition. But I'm not sure - the OP would need to clarify.
Keep it simple. Anyway, as you wish :p
I very much agree with keeping things simple. Does it get more simple than this?
ObjectSetString(0, EDIT_BOX, OBJPROP_TEXT, (string)value);
PS I'm not sure it is as I wish, it is just an academic discussion for me - I have no need for it!
I'm not sure I follow...?
...If we take it as a general request : "How to check if the content of a variable is numeric ?", the most elegant solution is using regular expression.