.NET Double.TryParse (num, format, cultureinfo, out) Error

Background: Recently, I was pleased to write code that was supposed to reliably convert strings to double strings internationally. This functionality also had to be distributed. those. the string was stored in the database and should be converted to a number by a number of agents working in different locales. For reasons of limiting changing the database schema, there was no question, and I had to do this work in the old code base with a simple update path and not break existing functions.

I was able to solve this by normalizing the saved string in an invariant format and adding a flag to the encoding to indicate whether the value was normalized, and whether it should accept a new path or non-normalized (sp?) And take the legacy path.

I forgot to mention that the original value is entered by the end user and should be in the range of valid formats. The value of the stored value may or may not have group qualifiers. Obviously this is dangerous, it is currently in beta only, and appropriately internationalizing the user interface will soon appear for a proper release.

I said that it is reasonable that my conversion code should be able to handle grouping characters, even if the final normalized form does not include them. Double.TryParse () and Double.ToString (), provided with the proper culture format, should not have problems with this, and the conversion code can be reused for other reasons (yay legacy code!).

.NET error . Therefore, I believe that it would be nice to write some unit tests around an internationalized string for double conversion code.

I am writing two main tests (kind of psuedo code).

Test1:

Double testValue = 15000.05
foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures) 
{
    string testString = testValue.ToString(ci);
    Assert.AreEqual(testValue, Convert(testString, ci));
}

Test2:

foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures) 
{
    string testString = testValue.ToString("N2", ci);
    Assert.AreEqual(testValue, Convert(testString, ci));
}

Corresponding conversion code (almost line to line):

If Not Double.TryParse(numIn, Globalization.NumberStyles.Any, cultureInfo, numOut) Then Return False

, Convert , . .ToString(ci) .ToString( "N2", ci). en-US "15000.05" "15,000.05" . , .NET 2.0 - 4.5.2, . (, , .NET 4.5.2)

1 !

Test2 5 :

  • PRS
  • PRS-AF
  • -Latn
  • -Latn-

, , , .

. .. . Double.TryParse()

numOut = Double.Parse(numIn, ci)

. , Double.TryParse() , , NumberStyle.Any. .

, .NET, double IFormatProvider, double, IFormatProvider, .

: - , ?

:. - ( , double x = 0,3 0.299... .NET).

: VB.NET #, , , . , , "" , , , 1 015 000 10,15,000.

+4
1

@tarekgh GitHub. :

" , , :

- "," - "." - "." . - "," , . , .

, , "15.000,05". , NumberStyles.Any, , . "." . . "," . , false TryParse ( Parse).

, NumberStyles. .

        Double.TryParse(numString, NumberStyles.Any & (~NumberStyles.AllowCurrencySymbol), ci, out numParsed);

, , . "

0

Source: https://habr.com/ru/post/1679312/


All Articles