Why decimal.TryParse successfully parses something like 12,2,2,2

If I do the following:

string teststring = "12,2,2,2";
decimal testdecimal;
decimal.TryParse(teststring, out testdecimal)

This works, and testdecimal ends as 12222.

Should this reasonably / logically not be understood? I was surprised to see that it worked, and I was curious what his logic is that allows it.

+4
source share
4 answers

Your culture settings have ,as thousands separator. Since the thousands separator is for visual signals only and does not contain any important quantity information, it decimal.TryParsesimply ignores the thousands separators.

+2
source

TryParse() , , .

, NumberStyles, NumberStyles.AllowThousands.

:

Number   = AllowLeadingWhite | AllowTrailingWhite | AllowLeadingSign | AllowTrailingSign |
           AllowDecimalPoint | AllowThousands,

Source

+6

- . . Decimal.TryParse . , . , ​​ , TryParse , , , .

        var d1 = Decimal.Parse("1,23,25"); //parse is successful
        Thread.CurrentThread.CurrentCulture = new CultureInfo("tr");
        d1 = Decimal.Parse("1,23,45"); //throws exception
+3

- Decimal. , , :

Decimal.Parse("12,2,2,2", NumberStyles.Number ^ NumberStyles.AllowThousands)

InvalidFormatException

+1

All Articles