Using NumberStyles to parse numbers in C# .NET

There are a lot of number formats out there depending on the industry we’re looking at. E.g. negative numbers can be represented in several different ways:

  • -14
  • (14)
  • 14-
  • 14.00-
  • (14,00)

…and so on. Accounting, finance and other, highly “numeric” fields will have their own standards to represent numbers. Your application may need to parse all these strings and convert them into proper numeric values. The static Parse method of the numeric classes, like int, double, decimal all accept a NumberStyles enumeration. This enumeration is located in the System.Globalization namespace.

The following bit of code will throw a FormatException:

string rawNumber = "(14)";
int parsed = int.Parse(rawNumber);

However, as soon as you add a special NumberStyles enumeration value the code will pass:

string rawNumber = "(14)";
int parsed = int.Parse(rawNumber, NumberStyles.AllowParentheses);

“parsed” will be correctly set to -14.

You can combine the enumeration flags. Say that you need to convert “$(14)”, i.e. -14 dollars. Here’s a possible solution:

int parsed = int.Parse(rawNumber, NumberStyles.AllowParentheses | NumberStyles.AllowCurrencySymbol);

Note that the above method may fail depending on the current culture settings of the thread our program is running in. If you’re running with the US culture then the $ sign will be accepted otherwise the above method will fail. My PC is set to the Swedish culture se-SV. The Swedish currency is abbreviated by “kr” so I actually got an exception when I ran the above method. In my case I had to modify the currency symbol to “kr” as follows:

string rawNumber = "kr(14)";
int parsed = int.Parse(rawNumber, NumberStyles.AllowParentheses | NumberStyles.AllowCurrencySymbol);

“parsed” was set to -14 as expected. This is not the only solution though.

It can be cumbersome to keep adding these flags to make sure all currency formats are covered. NumberStyles includes some composite values including NumberStyles.Currency. The Currency value combines all styles except for AllowExponent. If you start typing “NumberStyles.” in Visual Studio and investigate any of the values whose name does NOT start with “allow” then IntelliSense will show you the list of flags included in the composite. So the above currency example can be simplified as follows:

string rawNumber = "kr(14)";
int parsedNumber = int.Parse(rawNumber, NumberStyles.Currency);

We cannot only parse “normal” numbers, i.e. numbers of the decimal system but hexadecimal numbers as well. Consider the following example:

string rawNumber = "A2DB";
int parsedNumber = int.Parse(rawNumber, NumberStyles.HexNumber);

parsedNumber will be 41691.

There’s a neater solution where you can pass in an IFormatProvider. The CultureInfo class has a NumberFormat property of type NumberFormatInfo which implements a range of numeric format info such as the currency symbol, the thousands separator, the percentage format etc.:

CultureInfo americanCulture = new CultureInfo("en-US");
string rawNumberCurrency = "$(14)";
int parsedCurrency = int.Parse(rawNumberCurrency, NumberStyles.Currency, americanCulture.NumberFormat);

The above code passes on my PC as well.

The above technique can be used for custom number formats as well. Imagine that you found your own country called “The Software Republic” where the currency is denoted by ‘{}’ and the thousand delimiter is ‘;’, e.g. ” {}(13;000) “. Easy:

NumberFormatInfo softwareRepNumberFormat = new NumberFormatInfo()
{
	CurrencyGroupSeparator = ";"
	, CurrencySymbol = "{}"
};
string rawNumberCurrencyDev = " {}(13;000) ";
int parsedCurrencyDev = int.Parse(rawNumberCurrencyDev, NumberStyles.Currency, softwareRepNumberFormat);

parsedCurrencyDev will be correctly assigned -13000.

Occasionally negative numbers are shown with a trailing negative sign like this: “13-“. There’s a solution for that:

string number = "13-";
int parsed = int.Parse(number, NumberStyles.AllowTrailingSign);

“parsed” will be -13 as expected.

What if the string value has leading and trailing white space, like ” 13- “? Not a problem:

string number = " 13- ";
int parsed = int.Parse(number, NumberStyles.AllowTrailingSign | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingWhite);

“parsed” will be -13 again.

So far we’ve only looked at small numbers. Let’s go up to -13,000, i.e. ” 13,000- “. Here’s a solution:

string number = " 13,000- ";
int parsed = int.Parse(number, NumberStyles.AllowTrailingSign 
		| NumberStyles.AllowTrailingWhite 
		| NumberStyles.AllowLeadingWhite
		| NumberStyles.AllowThousands);

Note, however, that the above example will only work with cultures where thousands are delimited with a comma ‘,’, such as the US culture. In Sweden thousands are not separated by a comma, but by a white space: “13 000”. The above code actually fails on my PC but the one below passes and “parsed” will be set to -13000:

string number = " 13 000- ";
int parsed = int.Parse(number, NumberStyles.AllowTrailingSign 
	| NumberStyles.AllowTrailingWhite 
	| NumberStyles.AllowLeadingWhite
	| NumberStyles.AllowThousands);

View all various C# language feature related posts here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Using NumberStyles to parse numbers in C# .NET

  1. Miklos Kanyo says:

    Thanks a lot for this, very helpful

Leave a Reply to Miklos Kanyo Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: