Culture Invariant Decimal.TryParse ()

I am writing a custom string for a decimal validator that should use Decimal.TryParse, which ignores the culture (that is, it doesn't matter if the input contains "." Or "," as a decimal point separator). This is the suggested method:

public static bool TryParse( string s, NumberStyles style, IFormatProvider provider, out decimal result ) 

I can’t figure out what to use as the third parameter. The examples I saw are as follows:

 culture = CultureInfo.CreateSpecificCulture("en-GB"); Decimal.TryParse(value, style, culture, out number) 

to create a specific culture. CultureInfo does not have a CreateInvariantCulture method and CultureInfo.InvariantCulture is not a required type. What is the correct use?

+16
decimal c # cultureinfo tryparse
Apr 17 '14 at 11:03
source share
4 answers

Try it like this:

 decimal value; bool b = Decimal.TryParse("0.1", NumberStyles.Any, new CultureInfo("en-US"), out value); 

The best way would probably be to use the Decimal.Parse () method, as is traditional with any decimal string value.

You can use NumberStyles.Currency to indicate that the values ​​should be read as a currency that will take into account any values ​​related to the currency (you will need to add a link to System.Globalalization to use this:

 using System.Globalization; 

Decimal.Parse also accepts a third parameter, which will allow you to explicitly set IFormatProvider , if you so wish, and wish you a specific culture:

 decimal value = Decimal.Parse(currency, NumberStyles.Currency, CultureInfo.InvariantCulture); //yields 15.55 
+28
Apr 17 '14 at 11:05
source share

My bad guys. I checked the following code:

  string DutchDecimal = "1,5"; string EnglishDecimal = "1.5"; decimal a; decimal b; Console.WriteLine(decimal.TryParse(DutchDecimal, out a)); Console.WriteLine(a); Console.WriteLine(decimal.TryParse(EnglishDecimal, out b)); Console.WriteLine(b); Console.Read(); 

and it parses both lines correctly. By default, TryParse seems to be a culture-invariant. I assumed this was not the case because the standard TypeConversionValidator in EnterpriseLibrary was culture dependent, and I assumed that it just used TryParse. However, as it turned out, this default parser is hard-coded to use the current culture.

EDIT: I found out that “1.5” converts to 1.5 and “1.5” converts to 15. This is really correct for cultural behavior, so it is. This whole question, apparently, gave rise to my misunderstanding of how the cultural invariant works.

+5
Apr 17 '14 at 11:48
source share

In fact, CultureInfo.InvariantCulture can be used here. The parameter expects IFormatProvider , an interface that implements CultureInfo . But InvariantCulture is invariant in the sense that it does not depend on user settings.

In fact, there is no culture that accepts either, or . like a decimal separator - they are all one or the other. You will need to find another way to process the data that any of these delimiters can use.

+1
Apr 17 '14 at 11:05
source share

I can’t figure out what to use as the third parameter.

Because all cultures are NumberDecimalSeparator or NumberGroupSeparator , etc. do not match.

Someone is using . like NumberDecimalSeparator , someone uses , but there is no CultureInfo that uses both as NumberDecimalSeparator .

CultureInfo implements the IFormatProvider interface. Therefore, if you specify your CultureInfo , your value string will try to parse these crop rules.

I am writing a custom string in a decimal validator that should use Decimal.TryParse, ignoring the culture

In this case, you can use the CultureInfo.Clone method to copy which culture you want (or InvariantCulture ), and you can set NumberDecimalSeparator and NumberGroupSeparator, which string you want.

+1
Apr 17 '14 at 11:06
source share



All Articles