Read the syntax
Use NumberStyles and CultureInfo to distinguish machine data from culture-specific user input.
TryParse writes the decimal result and returns true for the supplied text.
What it is and why to use it
The text 12.50 can be a machine-formatted decimal, while another locale writes 12,50. Parsing should follow the data format’s contract. A culture provides conventions such as decimal separators; NumberStyles controls which extra symbols and whitespace the parser accepts. TryParse reports ordinary invalid input without using exceptions as the normal validation path.
How it works
Use an overload accepting both NumberStyles and an IFormatProvider such as CultureInfo.InvariantCulture. Check its boolean result before using the out value. InvariantCulture is appropriate for a format explicitly defined that way; it is not a substitute for a user’s locale when reading human-entered numbers. Formatting the result also needs a deliberate culture.
Common mistakes and limitations
Do not rely on the current culture for a portable configuration format. Do not strip all commas or dots to “fix” a number: that can change its value. Restrict accepted styles to those the format permits, and validate the resulting numeric range separately.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
decimal.TryParse(text, NumberStyles.Number, CultureInfo.InvariantCulture, out decimal value)Examples
Parse a portable decimal value
Use a fixed format and print with the same culture for predictable output.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Globalization;
string text = "12.50";
bool valid = decimal.TryParse(text, NumberStyles.AllowDecimalPoint,
CultureInfo.InvariantCulture, out decimal amount);
Console.WriteLine(valid);
Console.WriteLine(amount.ToString("F2", CultureInfo.InvariantCulture));
- AllowDecimalPoint accepts the dot required by this chosen invariant format.
- TryParse writes the decimal result and returns true for the supplied text.
- F2 prints two decimal places with an explicit culture. The example is about parsing, not a complete money-validation policy.
- Expected output: True 12.50
- Declare the culture and accepted symbols of interchange formats.
Read a specific user-input convention
Compare a French decimal with a narrowly defined invariant format.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Globalization;
string text = "12,50";
bool french = decimal.TryParse(text, NumberStyles.AllowDecimalPoint,
CultureInfo.GetCultureInfo("fr-FR"), out decimal value);
bool invariant = decimal.TryParse(text, NumberStyles.AllowDecimalPoint,
CultureInfo.InvariantCulture, out _);
Console.WriteLine(value.ToString("F2", CultureInfo.InvariantCulture));
Console.WriteLine($"French: {french}; invariant: {invariant}");
- The French culture recognises the comma as its decimal separator.
- The invariant call does not allow thousands separators, so it rejects this comma rather than interpreting it as grouping.
- The result illustrates why blindly trying many permissive cultures can accept an unintended value.
- Expected output: 12.50 French: True; invariant: False
- Declare the culture and accepted symbols of interchange formats.
Best practices
- Declare the culture and accepted symbols of interchange formats.
- Check both parse success and the application’s numeric range.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference