user437890
user437890

Reputation: 85

Casting using a decimal in C#

private int FindNumber(string sPar)
   {
      // Get the last number
      int len = sPar.Length;
      string s = sPar.Substring(len-1);
      return new (int)Decimal(s);
   }

In this I am getting the error ; required . Can any ine help me on this.

Thank You!

Upvotes: 1

Views: 321

Answers (7)

Khaniya
Khaniya

Reputation: 63

I suppose you want to convert string to decimal as your code is not very clear.

you can use

   Decimal d1;   
   if(Decimal.TryParse(sPar,out d1))    
   {
       return d1    
   }    
   else    
   {    
       return 0;    
   }

Upvotes: 0

vaitrafra
vaitrafra

Reputation: 662

The last line is completely wrong. Your code takes the last char of a string(that i presume is always a number) and cast it to an int.

Do the following

try{
return Int32.Parse(s);
}
catch(Exception e)
{
// manage conversion exception here
}

Upvotes: 0

LukeH
LukeH

Reputation: 269278

Do you just want to parse the digit from the last position in the string?

private int FindNumber(string sPar)
{
    return int.Parse(sPar.Substring(sPar.Length - 1));
}

Note that this will fail if (a) the string is null, (b) the string is empty, or (c) the last character of the string isn't a digit. You should add some checking to your method if these situations are likely to be a problem.

Upvotes: 0

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60684

I'm not 100% what you are trying to do here, but if you want to get 4 as the result from the string "17894" i guess you want to write it like this with minimum number of changes:

private int FindNumber(string sPar) { 
  // Get the last number 
  int len = sPar.Length; 
  string s = sPar.Substring(len-1);
  return int.Parse(s); 
}

No reason to include a decimal and parse it to an int if you are only taking one char of the string anyway.

Note that this will give an exception if the last char of the string for any reason is not a number.

Upvotes: 1

Onkelborg
Onkelborg

Reputation: 3987

Decimal(s) is not a valid call since Decimal is a type. Try Decimal.Parse(s) instead if you are certain that s is a valid decimal, stored as a string. If not, use Decimal.TryParse instead.

Please take into account different Culture related problems also, check out the overload that takes an IFormatProvider (I think, not sure about the exact name)

Upvotes: 0

Richard J. Ross III
Richard J. Ross III

Reputation: 55533

Change your code to this:

private int FindNumber(string sPar)
{
      int len = sPar.Length;
      string s = sPar.Substring(len - 1);
      return Convert.ToInt32(s);
}

Or, even shorter:

private int FindNumber(string sPar)
{
      return Convert.ToInt32(sPar.Substring(sPar.Length - 1));
}

Upvotes: 5

Marc Gravell
Marc Gravell

Reputation: 1062502

what is Decimal(s)? If you mean "parse as a decimal, then cast to int":

return (int)decimal.Parse(s);

If it is known to be an integer, just:

return int.Parse(s);

Actually, since you are only interested in the last digit, you could cheat:

private static int FindNumber(string sPar)
{
    char c = sPar[sPar.Length - 1];
    if (c >= '0' && c <= '9') return (int)(c - '0');
    throw new FormatException();
}

Upvotes: 0

Related Questions