Converting Null or Empty string to decimals

Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 1131
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

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

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

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