C# Decimal TryParse(String, NumberStyles, IFormatProvider, Decimal)
Description
Decimal TryParse(String, NumberStyles, IFormatProvider,
Decimal)
converts the string representation of a number to its Decimal
equivalent using the specified style and culture-specific format. A return
value indicates whether the conversion succeeded or failed.
Syntax
Decimal.TryParse(String, NumberStyles, IFormatProvider, Decimal)
has the following syntax.
public static bool TryParse(
string s,/*from w ww .j av a 2s. c o m*/
NumberStyles style,
IFormatProvider provider,
out decimal result
)
Parameters
Decimal.TryParse(String, NumberStyles, IFormatProvider, Decimal)
has the following parameters.
s
- The string representation of the number to convert.style
- A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is Number.provider
- An object that supplies culture-specific parsing information about s.result
- When this method returns, contains the Decimal number that is equivalent to the numeric value contained in s, if the conversion succeeded, or is zero if the conversion failed. The conversion fails if the s parameter is null or String.Empty, is not in a format compliant with style, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.
Returns
Decimal.TryParse(String, NumberStyles, IFormatProvider, Decimal)
method returns true if s was converted successfully; otherwise, false.
Example
The following example demonstrates the use of the TryParse(String, NumberStyles, IFormatProvider, Decimal) method to parse the string representation of a number that has a particular style and is formatted using the conventions of a particular culture.
using System;/* ww w .ja v a 2s . co m*/
using System.Globalization;
public class MainClass{
public static void Main(String[] argv){
string value;
NumberStyles style;
CultureInfo culture;
decimal number;
// Parse currency value using en-GB culture.
value = "?1,097.63";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
culture = CultureInfo.CreateSpecificCulture("en-GB");
if (Decimal.TryParse(value, style, culture, out number))
System.Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
System.Console.WriteLine("Unable to convert '{0}'.", value);
value = "1345,978";
style = NumberStyles.AllowDecimalPoint;
culture = CultureInfo.CreateSpecificCulture("fr-FR");
if (Decimal.TryParse(value, style, culture, out number))
System.Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
System.Console.WriteLine("Unable to convert '{0}'.", value);
value = "1.345,978";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
culture = CultureInfo.CreateSpecificCulture("es-ES");
if (Decimal.TryParse(value, style, culture, out number))
System.Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
System.Console.WriteLine("Unable to convert '{0}'.", value);
value = "1 345,978";
if (Decimal.TryParse(value, style, culture, out number))
System.Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
System.Console.WriteLine("Unable to convert '{0}'.", value);
}
}
The code above generates the following result.