C# Convert ToDecimal(Object, IFormatProvider)
Description
Convert ToDecimal(Object, IFormatProvider)
converts
the value of the specified object to an equivalent decimal number, using
the specified culture-specific formatting information.
Syntax
Convert.ToDecimal(Object, IFormatProvider)
has the following syntax.
public static decimal ToDecimal(
Object value,
IFormatProvider provider
)
Parameters
Convert.ToDecimal(Object, IFormatProvider)
has the following parameters.
value
- An object that implements the IConvertible interface.provider
- An object that supplies culture-specific formatting information.
Returns
Convert.ToDecimal(Object, IFormatProvider)
method returns A decimal number that is equivalent to value, or 0 (zero) if value is null.
Example
using System;/* w w w. j a v a 2 s. co m*/
using System.Globalization;
public class Example
{
public static void Main()
{
object[] values = { "123456789", "12345.6789", "12 345,6789",
"123,456.789", "123 456,789", "123,456,789.0123",
"123 456 789,0123" };
CultureInfo[] cultures = { new CultureInfo("en-US"),
new CultureInfo("fr-FR") };
foreach (CultureInfo culture in cultures)
{
Console.WriteLine("String -> Decimal Conversion Using the {0} Culture",
culture.Name);
foreach (string value in values)
{
Console.Write("{0,20} -> ", value);
try {
Console.WriteLine(Convert.ToDecimal(value, culture));
}
catch (FormatException) {
Console.WriteLine("FormatException");
}
}
Console.WriteLine();
}
}
}
The code above generates the following result.