Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Culture invariant Decimal.TryParse()

I'm writing a custom string to decimal validator that needs to use Decimal.TryParse that ignores culture (i.e. doesn't care if the input contains "." or "," as 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 3rd parameter. The examples I've seen look like this:

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

so they create a specific culture. CultureInfo does not have a "CreateInvariantCulture" method, and CultureInfo.InvariantCulture is not of the required type. What's the correct usage?

like image 806
Shaggydog Avatar asked Apr 17 '14 11:04

Shaggydog


People also ask

What does Decimal TryParse do?

TryParse(String, Decimal) Converts the string representation of a number to its Decimal equivalent. A return value indicates whether the conversion succeeded or failed.

How do you convert decimal to string?

To convert a Decimal value to its string representation using a specified culture and a specific format string, call the Decimal. ToString(String, IFormatProvider) method.

What is decimal TryParse?

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.

Does invariantculture work with decimal separators?

But InvariantCulture is invariant in the sense that it does not vary with the user's settings. In fact, there is no culture that accepts either , or . as decimal separator – they are all one or the other. You'll have to find some other way to deal with data which can use either of these decimal separators. My bad guys. I tested the following code:

Is the default TryParse in enterpriselibrary culture invariant?

Seems like the default TryParse is indeed culture invariant. I assumed this was not the case, because the default TypeConversionValidator in EnterpriseLibrary was culture dependent and I assumed it simply used TryParse. However, as it turns out this default parser is hardcoded to use current culture.

What does the return value indicates in TryParse ()?

A return value indicates whether the conversion succeeded or failed. Decimal.TryParse (String, NumberStyles, IFormatProvider, Decimal) has the following syntax.


Video Answer


2 Answers

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

In fact, there is no culture that accepts either , or . as decimal separator – they are all one or the other. You'll have to find some other way to deal with data which can use either of these decimal separators.

like image 195
David Heffernan Avatar answered Sep 17 '22 17:09

David Heffernan


My bad guys. I tested 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 correctly parses both strings. Seems like the default TryParse is indeed culture invariant. I assumed this was not the case, because the default TypeConversionValidator in EnterpriseLibrary was culture dependent and I assumed it simply used TryParse. However, as it turns out this default parser is hardcoded to use current culture.

EDIT: I found out that "1.5" converts to 1.5 and "1,5" converts to 15. This is actually correct for culture invariant behavior, so there it is. This whole question was apparently spawned by my misunderstanding of how culture invariant works.

like image 22
Shaggydog Avatar answered Sep 17 '22 17:09

Shaggydog