Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
Last active January 21, 2018 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankitvijay/d45a7ca0eb6dc421ba70b94815e48b37 to your computer and use it in GitHub Desktop.
Save ankitvijay/d45a7ca0eb6dc421ba70b94815e48b37 to your computer and use it in GitHub Desktop.
Number Styles Example
using System;
using System.Globalization;
namespace NumberStylesExample
{
public class Program
{
static void Main(string[] args)
{
// A Hexadecimal number with trailing and leading white spaces
Console.WriteLine(int.Parse(" AbCDeF ", NumberStyles.HexNumber |
NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite));
// A decimal number with -ve number defined within parentheses
Console.WriteLine(Decimal.Parse("(10000.23)", NumberStyles.Float |
NumberStyles.AllowParentheses));
// A slightly complex example which shows how NumberStyles can be used
// with TryParse method along with NumberFormatInfo.
var numberFormatInfo = new NumberFormatInfo();
numberFormatInfo.CurrencyDecimalSeparator = ",";
numberFormatInfo.CurrencyGroupSeparator = ".";
numberFormatInfo.CurrencySymbol = "£";
Double.TryParse("£120500,56", NumberStyles.Float | NumberStyles.Currency,
numberFormatInfo, out var result);
Console.WriteLine(result);
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment