Over a million developers have joined DZone.

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

SignalFx is the only real-time cloud monitoring platform for infrastructure, microservices, and applications. The platform collects metrics and traces across every component in your cloud environment, replacing traditional point tools with a single integrated solution that works across the stack.

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");
//int number = -1;
//bool isPased = int.TryParse("1234", out 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 |

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

// 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.

SignalFx is built on a massively scalable streaming architecture that applies advanced predictive analytics for real-time problem detection. With its NoSample™ distributed tracing capabilities, SignalFx reliably monitors all transactions across microservices, accurately identifying all anomalies. And through data-science-powered directed troubleshooting SignalFx guides the operator to find the root cause of issues in seconds.

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 }}