1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| static class Program
{
static void DecimalParse(NumberStyles styles,
IFormatProvider provider)
{
string[] decimalFormats = { "9876543210.9876543210",
"9876543210,9876543210", "(9876543210,9876543210)",
"9,876,543,210,987,654.3210",
"9.876.543.210.987.654,3210",
"98_7654_3210_9876,543210" };
// Parse each string in the decimalFormats array, using
// NumberStyles and IFormatProvider, if specified.
foreach (string decimalString in decimalFormats)
{
decimal decimalNumber;
// Display the first part of the output line.
Console.Write(" Parse of {0,-29}",
String.Format("\"{0}\"", decimalString));
try
{
// Use the appropriate Decimal.Parse overload,
// based on the parameters that are specified.
if (provider == null)
if (styles < 0)
decimalNumber =
Decimal.Parse(decimalString);
else
decimalNumber = Decimal.Parse(
decimalString, styles);
else
if (styles < 0)
decimalNumber = Decimal.Parse(
decimalString, provider);
else
decimalNumber = Decimal.Parse(
decimalString, styles, provider);
// Display the resulting value if Parse succeeded.
Console.WriteLine("succeeded: {0}",
decimalNumber);
}
catch (Exception ex)
{
// Display the exception message if Parse failed.
// Truncate it to fit on the output line.
Console.WriteLine("failed: {0}",
ex.Message.Substring(0, 31));
}
}
}
static void Main()
{
Console.WriteLine("This example of\n Decimal.Parse( " +
"String ),\n Decimal.Parse( String, NumberStyles " +
"),\n Decimal.Parse( String, IFormatProvider ), " +
"and\n Decimal.Parse( String, NumberStyles, " +
"IFormatProvider )\ngenerates the " +
"following output when run in the [{0}] culture.",
CultureInfo.CurrentCulture.Name);
Console.WriteLine("Several string representations " +
"of Decimal values are parsed.");
// IFormatProvider and NumberStyles are not used.
Console.WriteLine(
"\nNumberStyles and IFormatProvider are not " +
"used; current culture is [{0}]:",
CultureInfo.CurrentCulture.Name);
DecimalParse((NumberStyles)(-1), null);
// Use the NumberStyle for Currency.
Console.WriteLine("\nNumberStyles.Currency " +
"is used; IFormatProvider is not used:");
DecimalParse(NumberStyles.Currency, null);
// Create a CultureInfo object for another culture. Use
// [Dutch - The Netherlands] unless the current culture
// is Dutch language. In that case use [English - U.S.].
string cultureName =
CultureInfo.CurrentCulture.Name.Substring(0, 2) ==
"nl" ? "en-US" : "nl-NL";
CultureInfo culture = new CultureInfo(cultureName);
Console.WriteLine("\nNumberStyles is not used; [{0}] " +
"culture IFormatProvider is used:", culture.Name);
DecimalParse((NumberStyles)(-1), culture);
// Get the NumberFormatInfo object from CultureInfo, and
// then change the digit group size to 4 and the digit
// separator to '_'.
NumberFormatInfo numInfo = culture.NumberFormat;
numInfo.NumberGroupSizes = new int[] { 4 };
numInfo.NumberGroupSeparator = "_";
// Use the NumberFormatInfo object as the IFormatProvider.
Console.WriteLine("\nNumberStyles.Currency is used, " +
"group size = 4, separator = \"_\":");
DecimalParse(NumberStyles.Currency, numInfo);
}
} |
Partager