Select Page

The text 1,234 can mean 1234, mean 1.234, or fail to parse. The outcome depends on both the permitted syntax and the provider that interprets the punctuation. The three providers below produce two distinct numbers, not three.

This tutorial compares fifteen strings under en-US, de-DE and CultureInfo.InvariantCulture, using two explicit style sets. The complete console program targets .NET 10, uses no external packages, and was compiled and run in an isolated Linux SDK container. Its embedded expectations check all 90 cells.

Choose syntax and meaning separately

NumberStyles controls which elements are permitted; an IFormatProvider supplies their meaning. A CultureInfo provides numeric formatting information. Without an explicit provider, parsing uses the current culture. See Microsoft’s numeric parsing guide.

The two style sets used in this program
Label Style Permitted elements
F NumberStyles.Float Leading/trailing whitespace, a leading sign, decimal separator and exponent
W Float plus AllowThousands and AllowCurrencySymbol Everything in F, plus group separators and the provider’s currency symbol

W is just this program’s label. It is not an enum member. A flag permits an element without requiring it: both styles still accept plain digits. AllowDecimalPoint does not require a literal dot; the provider determines the separator. Microsoft lists the flags and composite styles in the NumberStyles reference.

Run the complete 90-cell comparison

Create a .NET 10 console project, replace its entire Program.cs with the code below, and run it:

dotnet new console --framework net10.0 --name NumericGrid
cd NumericGrid
dotnet run

The program needs en-US and de-DE culture data. It returns exit code 2 with an explanatory error if those cultures cannot be created or the checked separator and currency-symbol assumptions differ. It does not change the current culture. The currency row uses only $12.50, avoiding assumptions about German euro spacing. The expected grid describes the tested Linux environment, not every customized culture configuration.

using System;
using System.Globalization;

static class Program
{
    static int Main()
    {
        CultureInfo[] cultures;
        try
        {
            cultures = new[]
            {
                new CultureInfo("en-US", false),
                new CultureInfo("de-DE", false),
                CultureInfo.InvariantCulture
            };
        }
        catch (CultureNotFoundException)
        {
            Console.Error.WriteLine("This grid requires en-US and de-DE culture data.");
            return 2;
        }

        if (cultures[0].NumberFormat.NumberDecimalSeparator != "." ||
            cultures[0].NumberFormat.NumberGroupSeparator != "," ||
            cultures[0].NumberFormat.CurrencySymbol != "$" ||
            cultures[1].NumberFormat.NumberDecimalSeparator != "," ||
            cultures[1].NumberFormat.NumberGroupSeparator != ".")
        {
            Console.Error.WriteLine("Culture data differs from this grid's assumptions.");
            return 2;
        }

        NumberStyles[] styles =
        {
            NumberStyles.Float,
            NumberStyles.Float | NumberStyles.AllowThousands |
                NumberStyles.AllowCurrencySymbol
        };
        string[] inputs =
        {
            "1234", "1,234", "1.234", "1,234.5", "1.234,5",
            "12.5", "12,5", "-42", " 42 ", "1e3",
            "1,2e2", "$12.50", "12,34,567", "oops", ""
        };
        // Columns: en-US F/W, de-DE F/W, invariant F/W.
        string[] expected =
        {
            "1234|1234|1234|1234|1234|1234",
            "FAIL|1234|1.234|1.234|FAIL|1234",
            "1.234|1.234|FAIL|1234|1.234|1.234",
            "FAIL|1234.5|FAIL|FAIL|FAIL|1234.5",
            "FAIL|FAIL|FAIL|1234.5|FAIL|FAIL",
            "12.5|12.5|FAIL|125|12.5|12.5",
            "FAIL|125|12.5|12.5|FAIL|125",
            "-42|-42|-42|-42|-42|-42",
            "42|42|42|42|42|42",
            "1000|1000|1000|1000|1000|1000",
            "FAIL|1200|120|120|FAIL|1200",
            "FAIL|12.5|FAIL|FAIL|FAIL|FAIL",
            "FAIL|1234567|FAIL|FAIL|FAIL|1234567",
            "FAIL|FAIL|FAIL|FAIL|FAIL|FAIL",
            "FAIL|FAIL|FAIL|FAIL|FAIL|FAIL"
        };

        Console.WriteLine("input | en-US F | en-US W | de-DE F | de-DE W | invariant F | invariant W");
        int cells = 0;
        int mismatches = 0;
        for (int row = 0; row < inputs.Length; row++)
        {
            string[] expectedCells = expected[row].Split('|');
            string[] actualCells = new string[6];
            int column = 0;
            foreach (CultureInfo culture in cultures)
            {
                foreach (NumberStyles style in styles)
                {
                    bool ok = double.TryParse(inputs[row], style, culture, out double value);
                    string actual = ok
                        ? value.ToString("R", CultureInfo.InvariantCulture)
                        : "FAIL";
                    actualCells[column] = actual;
                    if (actual != expectedCells[column])
                    {
                        Console.Error.WriteLine($"Mismatch at row {row}, column {column}: expected {expectedCells[column]}, got {actual}");
                        mismatches++;
                    }
                    column++;
                    cells++;
                }
            }
            Console.WriteLine($"\"{inputs[row]}\" | {string.Join(" | ", actualCells)}");
        }
        Console.WriteLine($"Checked {cells} cells; mismatches: {mismatches}");
        return cells == 90 && mismatches == 0 ? 0 : 1;
    }
}

Read the results

The asserted application stdout is exactly the following. Quotes make the empty and whitespace-padded inputs visible. Successful values are formatted with invariant culture, so display punctuation does not conceal differences in parsing.

input | en-US F | en-US W | de-DE F | de-DE W | invariant F | invariant W
"1234" | 1234 | 1234 | 1234 | 1234 | 1234 | 1234
"1,234" | FAIL | 1234 | 1.234 | 1.234 | FAIL | 1234
"1.234" | 1.234 | 1.234 | FAIL | 1234 | 1.234 | 1.234
"1,234.5" | FAIL | 1234.5 | FAIL | FAIL | FAIL | 1234.5
"1.234,5" | FAIL | FAIL | FAIL | 1234.5 | FAIL | FAIL
"12.5" | 12.5 | 12.5 | FAIL | 125 | 12.5 | 12.5
"12,5" | FAIL | 125 | 12.5 | 12.5 | FAIL | 125
"-42" | -42 | -42 | -42 | -42 | -42 | -42
" 42 " | 42 | 42 | 42 | 42 | 42 | 42
"1e3" | 1000 | 1000 | 1000 | 1000 | 1000 | 1000
"1,2e2" | FAIL | 1200 | 120 | 120 | FAIL | 1200
"$12.50" | FAIL | 12.5 | FAIL | FAIL | FAIL | FAIL
"12,34,567" | FAIL | 1234567 | FAIL | FAIL | FAIL | 1234567
"oops" | FAIL | FAIL | FAIL | FAIL | FAIL | FAIL
"" | FAIL | FAIL | FAIL | FAIL | FAIL | FAIL
Checked 90 cells; mismatches: 0

Follow the 1,234 row. F rejects it under en-US because comma grouping is not enabled. W accepts it as 1234. Under de-DE, comma is the decimal separator, so both styles produce 1.234. Invariant culture agrees with en-US for this row.

The 1.234 row reverses the punctuation roles: German W produces 1234, while German F fails. The two mixed-separator rows show why deleting punctuation is unsafe: an English grouping-and-decimal spelling and a German spelling need different providers.

The scientific row 1,2e2 is especially revealing. German parsing gives 120; English W gives 1200 after accepting the comma as grouping. Both are successful conversions. Trying another culture only after failure cannot discover which interpretation the writer intended.

The dollar row succeeds only in English W. Enabling currency symbols does not accept every currency symbol under every provider. The irregularly grouped 12,34,567 also succeeds in English W and invariant W in this run. Do not use this parse success as proof that grouping matches your application’s required written format.

Turn the experiment into an input contract

The program calls the Double.TryParse string/style/provider overload and checks its Boolean result before displaying the value. A failed parse becomes FAIL, rather than a misleading numeric zero. Every cell is compared with a separately written expected value; a mismatch prints its location and makes the process fail. The final count also has to be 90.

For user-entered numbers, select the culture the input form promises. For machine-generated text, define a format contract and use invariant culture on both sides when that contract is invariant. Choose only the syntax that contract allows; omitting group and currency flags avoids accepting those forms. Do not guess the culture by trying parsers until one succeeds.

A successful conversion still needs your application’s value checks. In modern .NET, values beyond the finite double range can parse to infinity; the Double.TryParse reference documents this behavior from .NET Core 3.0 onward. This grid deliberately uses small finite values and is not a money, range-validation or exhaustive parsing test.

For the conversion basics, continue with converting a string to an int in C#. For a complementary testing approach, see property-based tests for parsing code, which checks an invariant integer contract.

Share This