How do I check if the content of a variable is numeric? - page 4

 
Alain Verleyen:
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: 

honest_knave:

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. 

 
honest_knave:

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. 

That's why I said "and a processing of special case 0."

Simplified example :
   double value=StringToDouble(inputs);
   if(value==0)
     {
      if(inputs=="0" || inputs=="0.0")
        {
         //--- all is ok                
        }
      else
        {
         //--- wrong inputs
        }
     }
 
Alain Verleyen:
That's why I said "and a processing of special case 0."

Simplified example :
   double value=StringToDouble(inputs);
   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?


honest_knave:

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. 

 
honest_knave:

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.

 
Alain Verleyen:
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! 

 
honest_knave:

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! 

You could also ask to input zero as "0" and reject all other cases.

Or using regular expression :-D
 
Alain Verleyen:
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.

#property strict

#define EDIT_BOX "EditBox"

int OnInit()
  {
   ObjectCreate    (0, EDIT_BOX, OBJ_EDIT000);
   ObjectSetInteger(0, EDIT_BOX, OBJPROP_XDISTANCE200);
   ObjectSetInteger(0, EDIT_BOX, OBJPROP_YDISTANCE200);
   ObjectSetInteger(0, EDIT_BOX, OBJPROP_XSIZE100);
   ObjectSetInteger(0, EDIT_BOX, OBJPROP_YSIZE,  20);
   ObjectSetInteger(0, EDIT_BOX, OBJPROP_ALIGNALIGN_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);
     }
  }
 
honest_knave:

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.

#property strict

#define EDIT_BOX "EditBox"

int OnInit()
  {
   ObjectCreate    (0, EDIT_BOX, OBJ_EDIT000);
   ObjectSetInteger(0, EDIT_BOX, OBJPROP_XDISTANCE200);
   ObjectSetInteger(0, EDIT_BOX, OBJPROP_YDISTANCE200);
   ObjectSetInteger(0, EDIT_BOX, OBJPROP_XSIZE100);
   ObjectSetInteger(0, EDIT_BOX, OBJPROP_YSIZE,  20);
   ObjectSetInteger(0, EDIT_BOX, OBJPROP_ALIGNALIGN_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

 
Alain Verleyen:

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.

 

Alain Verleyen:

Keep it simple. Anyway, as you wish :p

 I very much agree with keeping things simple. Does it get more simple than this?

      double value = (double) ObjectGetString(0, EDIT_BOX, OBJPROP_TEXT);
      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!

 
honest_knave:

I'm not sure I follow...?

...
Quite honestly I have no idea about the OP's intention
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.