Why is it impossible to group pairs parses. Int.TryParse (but double.TryParse can)?

In C #, why can't you group the number of parsers int.TryParse (but double.TryParse can)?

        int i1 = 13579;
        string si1 = i1.ToString("N0");  //becomes 13,579
        int i2 = 0;
        bool result1 = int.TryParse(si1, out i2); //gets false and 0

        double d1 = 24680.0;
        string sd1 = d1.ToString("N0"); //becomes 24,680
        double d2 = 0;
        bool result2 = double.TryParse(sd1, out d2); //gets true and 24680.0

???

+4
source share
2 answers

Because the conversion coefficient of both data types is different. the parameter assigned by the string value will be different from both data types.

Int.TryParse does not contain a parameter specific to thousands of thousands of delimiters in the form of a conversion parameter

eg

in Int.TryParse parameter form

[ws][sign]digits[ws]

ws: White space (optional)
sign: An optional Sign (+-)
digit: sequance of digit (0-9)

and in Decimal.TryParse the parameter form

[ws][sign][digits,]digits[.fractional-digits][ws]

ws: White space (optional)
sign: An optional Sign (+-)
digit: sequance of digit (0-9)
,: culture specific thousand separator
.: culture specific decimal point.
fractional-digits: fractional digit after decimal point.

msdn. Int.TryParse Decimal.TryParse

+3

NumberStyles, .

, , Parse TryParse .

true i2:

bool result1 = int.TryParse(si1,
   NumberStyles.AllowThousands, CultureInfo.CurrentCulture.NumberFormat, out i2);

NumberStyles. , NumberStyles.Number , , ..


int.TryParse ( ) NumberStyles.Integer, , .

double.TryParse NumberStyles.Float| NumberStyles.AllowThousands, , , .

+5

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


All Articles