Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#
Article

Check If A String Value Is Numeric

Rate me:
Please Sign up or sign in to vote.
4.36/5 (39 votes)
6 Mar 2006CPOL2 min read 426.9K   45   26
A way to determine if a string value has a specific Style Number And/OR Culture

Introduction

This article will give you a way to determine if a given string has a valid number style (Integer, Float, Currency, etc.) according to specified/current culture.

Background

In all VBScript and Visual Basic applications, there is a built-in function called IsNumeric / IsCurrency, but in C# there is no built-in function with the same functionality.
I have investigated a little and found out that all given solutions always use try catch to find out if the given string value can be parsed.

That seemed like a good way to test it, BUT what if the number contains commas or a currency sign?
Then I have to add another check to this code and strip down the non numeric sign and test the result.
Here I present a simple way that is supported by C#.

The Solution

In the code shown below, I actually use a built-in functionality of C# to determine if a given string value stands up to the requirements I want.

C#
public bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
{
    Double result;
    return Double.TryParse(val,NumberStyle,
		System.Globalization.CultureInfo.CurrentCulture,out result);
}

The above function allow me to test if the given string stands up to one or more of the following styles:

  • Hex Number
  • Number
  • Currency
  • Float

A more detailed list and explanation regarding System.Globalization.NumberStyles can be found here.

Examples

If you want to test for an integer number, then do the following:

C#
isNumeric("42000", System.Globalization.NumberStyles.Integer)

If you want to test for an integer number separated with commas, then do the following:

C#
isNumeric("42,000", System.Globalization.NumberStyles.Integer | 
	System.Globalization.NumberStyles.AllowThousands)

Using Other Cultures

I use the current culture as shown in the code below.
A list of all available culture names can be obtained here.

C#
public bool isNumeric
    (string val, System.Globalization.NumberStyles NumberStyle, string CultureName)
{
    Double result;
    return Double.TryParse(val,NumberStyle,new System.Globalization.CultureInfo
				(CultureName),out result);
}

There are many ways to approach this problem, I chose the one that suits beginner levels and does not involve any Regular Expression which requires a higher level of expertise.

If you are interested in a solution that involves Regular Expressions, please take a look at the following links:

Enjoy and tell me your findings.

History

  • 7th March, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Joezer BH1-Sep-13 20:45
professionalJoezer BH1-Sep-13 20:45 
QuestionSimple VBScript program to Validate Numeric value Pin
Gauthamvit200310-Jul-11 7:46
Gauthamvit200310-Jul-11 7:46 
AnswerRe: Simple VBScript program to Validate Numeric value Pin
Joezer BH1-Sep-13 20:46
professionalJoezer BH1-Sep-13 20:46 
GeneralRegEx for this function [modified] Pin
Anthony Urwin31-Jan-18 15:37
Anthony Urwin31-Jan-18 15:37 
GeneralRe: RegEx for this function [modified] Pin
Member 416585117-Apr-14 3:24
Member 416585117-Apr-14 3:24 
GeneralRe: RegEx for this function [modified] Pin
PIEBALDconsult31-Jan-18 15:42
mvePIEBALDconsult31-Jan-18 15:42 
QuestionDecimals? Pin
smcirish13-Jan-10 3:06
smcirish13-Jan-10 3:06 
AnswerRe: Decimals? Pin
Joezer BH1-Sep-13 20:47
professionalJoezer BH1-Sep-13 20:47 
GeneralYes yes yes Pin
L C D8-Dec-08 15:04
L C D8-Dec-08 15:04 
GeneralRe: Yes yes yes Pin
Dush Abe26-Jun-13 12:03
Dush Abe26-Jun-13 12:03 
GeneralAnother way A one-liner too :) Pin
Husko25-Mar-08 10:00
Husko25-Mar-08 10:00 
Questionhow to check if string is int and return boolean Pin
max_dev2006@yahoo.com11-Oct-06 22:58
max_dev2006@yahoo.com11-Oct-06 22:58 
AnswerRe: how to check if string is int and return boolean Pin
Ghecco17-Oct-06 5:31
Ghecco17-Oct-06 5:31 
GeneralRe: how to check if string is int and return boolean Pin
tfaiz20-Jun-11 22:29
tfaiz20-Jun-11 22:29 
GeneralWorking for me Pin
ThisTrain7-Mar-06 5:29
ThisTrain7-Mar-06 5:29 
GeneralGood Thinking Pin
ThisTrain7-Mar-06 5:17
ThisTrain7-Mar-06 5:17 
GeneralNo, no, no Pin
NormDroid7-Mar-06 4:35
professionalNormDroid7-Mar-06 4:35 
GeneralRe: No, no, no Pin
Marc Brooks7-Mar-06 4:41
Marc Brooks7-Mar-06 4:41 
GeneralRe: No, no, no Pin
NormDroid7-Mar-06 8:49
professionalNormDroid7-Mar-06 8:49 
GeneralRe: No, no, no Pin
NormDroid7-Mar-06 8:49
professionalNormDroid7-Mar-06 8:49 
GeneralRe: No, no, no Pin
kingbin7-Mar-06 5:54
kingbin7-Mar-06 5:54 
GeneralRe: No, no, no Pin
NormDroid7-Mar-06 8:49
professionalNormDroid7-Mar-06 8:49 
GeneralRe: No, no, no Pin
cerberiukas14-Mar-06 4:25
cerberiukas14-Mar-06 4:25 
AnswerRe: No, no, no Pin
Leon Kovach18-Mar-06 22:57
Leon Kovach18-Mar-06 22:57 
GeneralRe: No, no, no Pin
dave78638313-Aug-07 7:30
dave78638313-Aug-07 7:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.