Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Parsing Numbers With NumberStyles

DZone's Guide to

Parsing Numbers With NumberStyles

Learn a method for parsing string values in C# code using overload, which helps you avoid run-time exceptions.

· Performance Zone ·
Free Resource

xMatters delivers integration-driven collaboration that relays data between systems, while engaging the right people to proactively resolve issues. Read the Monitoring in a Connected Enterprise whitepaper and learn about 3 tools for resolving incidents quickly.

Parsing string values to numbers, i.e. converting a string representation of a number into a number type, is a basic task and most programming languages provide support to perform it easily. But sometimes, the developer writes too much complex code to perform this (string to number parsing) as they are not aware of the easy way provided by the programming language.

Below is a small tip about parsing the string representation of a number to a number type with respect to the C# language. C# has the below numeric types:

  • Integer types (sbyte, short, int, long, byte, ushort, uint, ulong),
  • Non-integer types (number with precision or floating) types (float, decimal)

To convert a string value in one of the above numeric types, C# provides two methods.

The two above methods allow us to convert a string representation of a number to a string. For example:

int number = int.Parse("1234");
//or
//int number = -1;
//bool isPased = int.TryParse("1234", out number);
Console.WriteLine(number);


But, it fails (throws a run-time exception) when you try an input like below:

int parsedNumber = int.Parse(" -1234 ");
int parsedNumber = int.Parse("(1234)");
int parsedNumber = int.Parse("$1234");
int parsedNumber = int.Parse("1,234");
int parsedNumber = int.Parse(" (1,234) ")


The input above throws s run-time exception, but the solution is to make use of overload of Parse and TryParse, which comes with NumberStyles as input. Below is a screenshot of overload of the Parse method:

The code below makes use of overload to avoid run-time exceptions.

//output: -1234
int parsedNumber = int.Parse("(1234)", NumberStyles.AllowParentheses);

// output: 1234
int parsedNumber = int.Parse("1,234", NumberStyles.AllowThousands);

// output: -1234
int parsedNumber = int.Parse(" -1234  ", NumberStyles.AllowTrailingWhite |
                                              NumberStyles.AllowLeadingWhite |
                                              NumberStyles.AllowLeadingSign);

// output: -1234
int parsedNumber = int.Parse(" (1,234) ", NumberStyles.AllowTrailingWhite |
                                          NumberStyles.AllowLeadingWhite |
                                     NumberStyles.AllowParentheses |
                                          NumberStyles.AllowThousands);

// allows currency symbol
// output: 1234
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
int parsedNumber4 = int.Parse("$1234", NumberStyles.Currency);


Different numbers allow different input values. As given in the above code, one can put logic OR different values of NumberStyles and allow the combination to permit various input formats.

Try out other values of NumberStyles listed at MSDN.

Discovering, responding to, and resolving incidents is a complex endeavor. Read this narrative to learn how you can do it quickly and effectively by connecting AppDynamics, Moogsoft and xMatters to create a monitoring toolchain.

Topics:
performance ,tutorial ,c# ,parsing ,number strings

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}