Converting Null or Empty string to decimals

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 1343
Suppose we have a string that can have null or blank values or can have a valid value also. We need to convert that to decimal. the below are the ways to do so

a)
string str = "";
decimal d= String.IsNullOrEmpty(str)?0M: Convert.ToDecimal(str);


b)
string str = "";
decimal d= String.IsNullOrEmpty(str)?default(decimal) : Convert.ToDecimal(str);


c)
string str = ""; 
decimal number;
decimal d= Decimal.TryParse (str , out number) ? number : default(decimal);


Hope this will be helpful

Comments or Responses

Login to post response