Wednesday, December 20, 2017

float.TryParse in C#

float.TryParse method is used to convert given string to its single-precision floating-point number same like float.Parse but the main difference is that it does not throw exception even if the input is not in correct format or is null.

float.TryParse(string str, out returnValue);

str is Input string.
returnValue: floating-point number equivalent to “str”.

If the conversion is successful, the given string in converted into floating-point number otherwise 0.0 is returned to the out parameter.

This function returns true for successful conversion and false if conversion is failed. Every time there will be one of two results either 0.0 if failed or value if successful. There will not be any type of exception thrown by this function even if the input string is:

a)   Null                                      :        NO ArgumentNullException
b)   Not is correct format            :        NO FormatException
c)    Out of range                         :        NO OverflowException

Share This
Previous Post
Next Post

TekGrabs, established in 2017, is in the business of sharing Information. It provides news, articles and photos about the different technologies, inventions in different domains. Provides information on programming and databases.