Click here to Skip to main content
Licence CPOL
First Posted 6 Mar 2006
Views 252,298
Bookmarked 41 times

Check If A String Value Is Numeric

By | 6 Mar 2006 | Article
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.

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:

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

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

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.

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)

About the Author

Leon Kovach

Web Developer

Israel Israel

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionSimple VBScript program to Validate Numeric value PinmemberGauthamvit20037:46 10 Jul '11  
GeneralRegEx for this function [modified] PinmemberAnthony Urwin0:24 8 Sep '10  
QuestionDecimals? Pinmembersmcirish3:06 13 Jan '10  
GeneralYes yes yes PinmemberL C D15:04 8 Dec '08  
GeneralAnother way A one-liner too :) PinPopularmemberHusko10:00 25 Mar '08  
Questionhow to check if string is int and return boolean Pinmembermax_dev2006@yahoo.com22:58 11 Oct '06  
AnswerRe: how to check if string is int and return boolean PinmemberGhecco5:31 17 Oct '06  
GeneralRe: how to check if string is int and return boolean Pinmembertfaiz22:29 20 Jun '11  
GeneralWorking for me PinmemberThisTrain5:29 7 Mar '06  
GeneralGood Thinking PinmemberThisTrain5:17 7 Mar '06  
GeneralNo, no, no Pinmembernorm.net4:35 7 Mar '06  
GeneralRe: No, no, no PinmemberMarc Brooks4:41 7 Mar '06  
GeneralRe: No, no, no Pinmembernorm.net8:49 7 Mar '06  
GeneralRe: No, no, no Pinmembernorm.net8:49 7 Mar '06  
GeneralRe: No, no, no Pinmemberkingbin5:54 7 Mar '06  
GeneralRe: No, no, no Pinmembernorm.net8:49 7 Mar '06  
GeneralRe: No, no, no Pinmembercerberiukas4:25 14 Mar '06  
AnswerRe: No, no, no PinmemberLeon Kovach22:57 18 Mar '06  
GeneralRe: No, no, no Pinmemberdave7863837:30 13 Aug '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 7 Mar 2006
Article Copyright 2006 by Leon Kovach
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid