[UWP] Decimal.Parse() throws FormatException after Windows reinstall

Aria Rad 51 Reputation points
2023-01-15T15:14:34.71+00:00

Hello,

Due to an unrelated issue my computer had to undergo a full wipe and reinstall of Windows (10 Pro) and all software during the past 3 weeks.

Now Decimal.Parse() throws a FormatException when parsing strings like "0.01" in the same UWP app that I was editing before.

At first I thought maybe it had something to do with my Regional formats that had changed after the reinstall, so I checked to make sure the formats match up with what I had previously. My system's Display language, Input language and Format are all in English - US, like before. I am in the Netherlands where the comma (,) is used as a decimal sign (.).

Example snippet:

<ComboBox Header="Unit" PlaceholderText="Please select" MaxWidth="250" 
                              HorizontalAlignment="Left" MinWidth="130"
                              x:Name="UnitCombo" 
                              Margin="10,0,0,0" 
                              VerticalAlignment="Bottom">
            <ComboBoxItem Tag="1000">kilometer</ComboBoxItem>
            <ComboBoxItem Tag="1">meter</ComboBoxItem>
            <ComboBoxItem Tag="0.1">decimeter</ComboBoxItem>
            <ComboBoxItem Tag="0.01">centimeter</ComboBoxItem>
            <ComboBoxItem Tag="0.001">millimeter</ComboBoxItem>
            <ComboBoxItem Tag="1E-6">micrometer</ComboBoxItem>
            <ComboBoxItem Tag="0.9144">yard</ComboBoxItem>
            <ComboBoxItem Tag="0.3048">foot</ComboBoxItem>
            <ComboBoxItem Tag="0.0254">inch</ComboBoxItem>
        </ComboBox>
decimal texttoparse = Decimal.Parse(((ComboBoxItem)UnitCombo.SelectedItem).Tag.ToString(),
                        NumberStyles.AllowDecimalPoint |
                        NumberStyles.AllowLeadingSign |
                        NumberStyles.AllowExponent);

I can fix this by using the overridden Parse() method that adds CultureInfo.InvariantCulture, but this causes a user input of "1,0" to be interpreted as 10.

Does anyone know what could have caused this change in behavior in the first place? I suspect it still has to do with my regional format, but I can't find what it could be.

Or was anything changed with how the Parse() method accepts input in the last month?

Developer technologies | Universal Windows Platform (UWP)
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C#
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.