CSharp examples for System:Converter
Convert input value object to decimal.
using System.Globalization; using System;/*from ww w.j a v a 2 s. c o m*/ public class Main{ /// <summary> /// Convert input value object to decimal. /// </summary> /// <param name="value">The value.</param> /// <param name="defaultValue">The default value.</param> /// <param name="provider">The provider.</param> /// <returns>The converted value.</returns> public static decimal AsDecimal(object value, decimal defaultValue, IFormatProvider provider) { if (value == null || value == DBNull.Value) { return defaultValue; } decimal retval; if (provider == null) { provider = CultureInfo.CurrentCulture; } try { retval = Convert.ToDecimal(value, provider); } catch (InvalidCastException) { retval = defaultValue; } catch (FormatException) { retval = defaultValue; } return retval; } /// <summary> /// Convert input value object to decimal. /// </summary> /// <param name="value">The value.</param> /// <param name="provider">The provider.</param> /// <returns>The converted value.</returns> public static decimal AsDecimal(object value, IFormatProvider provider) { return AsDecimal(value, 0, provider); } /// <summary> /// Convert input value object to decimal. /// </summary> /// <param name="value">The value.</param> /// <param name="defaultValue">The default value.</param> /// <returns>The converted value.</returns> public static decimal AsDecimal(object value, decimal defaultValue) { return AsDecimal(value, defaultValue, CultureInfo.CurrentCulture); } /// <summary> /// Convert input value object to decimal. /// </summary> /// <param name="value">The value.</param> /// <returns>The converted value.</returns> public static decimal AsDecimal(object value) { return AsDecimal(value, 0, CultureInfo.CurrentCulture); } }