Last active
January 21, 2018 07:09
-
-
Save ankitvijay/d45a7ca0eb6dc421ba70b94815e48b37 to your computer and use it in GitHub Desktop.
Number Styles Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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