c#将字符串类型转化为数字类型

来源:百度知道 编辑:UC知道 时间:2024/06/08 10:58:44

以下是我自己写的方法

/// <summary>
/// Covert the string to the int. If failed, return the apointed value.
/// </summary>
/// <param name="str">The string is going to be coverted.</param>
/// <param name="FailReturn">Will return this number when covert failed.</param>
/// <returns></returns>
public static int TryInt(string str, int FailReturn) {
int re_f = FailReturn;

try { re_f = int.Parse(str); }
catch { }

return re_f;
}

/// <summary>
/// Covert the string to the float. If failed, return the apointed value.
/// </summary>
/// <param name="str">The string is going to be coverted.</param>
/// <returns></returns>