How to do "IsNumber"?

 

Hi! I'd like test whether the OrderComment is a convertible from string to a number or not, but I can't find anything like an IsNumber() function. Is it possible to get around this?

Basically I need the code to test wehter OrderComment() is of type "1.3650" or "BALANCER" in order for the code to make better choices on what to do with that specific order.

Would appriciate any help!.


Re

Dennis

 
StrToInteger( text ) or  StrToDouble( text )
 
sgude0:I can't find anything like an IsNumber() function.
So make one.
Just typed, not compiled, not tested.
bool IsNumber(string s){
  for(int iPos = StringLen(s) - 1; iPos >= 0; iPos--){
     int c = StringGetCharacter(s, iPos);
     if( (c < '0' || c > '9') && c != '.') return false;
  }
  return true;
}
Just typed, not compiled, not tested.
 
William Roeder #:
So make one.
Just typed, not compiled, not tested.Just typed, not compiled, not tested.

hi, it work.

thanks a lot.

 
Also not tried or tested
bool IsNumber(string s)
{
  return MathIsValidNumber(StringToDouble(s));
}
 
Amir Yacoby #: Also not tried or tested

Will not work. StringToDouble("123XXX") returns 123.0, which is a valid number.

 
William Roeder #:

Will not work. StringToDouble("123XXX") returns 123.0, which is a valid number.

Ok, thought he means only numbers are in comment.

But you skipped some cases as well (:

"1.2.3.4" will return true

"....." will return true

etc..

 
bool IsNumber(string s)
{

bool sonuc=false;

int as_int = (int) s; // IsNumeric

if(as_int !=0 && MathIsValidNumber(as_int)) sonuc=true; 

return sonuc;

  //return MathIsValidNumber(StringToDouble(s));
}
 
Mehmet Ozhan Hastaoglu #:
  1. Code fails per #5 and #6 and all 0.wxyz.
  2. Code, simplified.
    bool IsNumber(string s){
       int as_int = (int) s; return as_int !=0 && MathIsValidNumber(as_int);
    }
 

William Roeder #So make one.

Just typed, not compiled, not tested.
bool IsNumber(string s){
  for(int iPos = StringLen(s) - 1; iPos >= 0; iPos--){
     int c = StringGetCharacter(s, iPos);
     if( (c < '0' || c > '9') && c != '.') return false;
  }
  return true;
}
Just typed, not compiled, not tested.


Including the point counter:

bool IsNumber(string s)
  {
   int p = 0;
   for(int iPos = StringLen(s) - 1; iPos >= 0; iPos --)
     {
      int c = StringGetCharacter(s, iPos);
      if(c == '.')
        {
         p ++;
         if(p > 1 || StringLen(s) < 2)
           {
            return false;
           }
        }
      else
        {
         if(c < '0' || c > '9')
           {
            return false;
           }
        }
     }
   return true;
  }
 

Tested and use in my projects.

//+------------------------------------------------------------------+
//| CHECK IF A TEXT IS A NUMBER - NGUYEN VAN ANH                     |
//+------------------------------------------------------------------+
bool IsNumber(string text)
{
   int length = StringLen(text);
   int pointcount = 0;

   //Xét từng ký tự trong văn bản
   for(int i = 0; i < length; i++)
   {
      //Lẫy mã thứ tự của ký tự
      int char1 = StringGetChar(text, i);
      
      //Nếu là dấu chấm thì tăng bộ đếm dấu chấm thêm 1 đơn vị
      if (char1 == 46) pointcount += 1;
   	
      //Nếu là ký tự số và số dấu chấm nhỏ hơn 2 thì xét kí tự kế tiếp, nếu không thì trả kết quả false
      if (((char1 > 47 && char1 < 58) || char1 == 46) && pointcount < 2) continue;
      else return(false);
   }

   //Nếu đã xét hết kí tự trong văn bản mà không bị trả kết quả false thì trả kết quả true
   return(true);
}
Reason: