Click here to Skip to main content
11,492,914 members (61,665 online)
Click here to Skip to main content

Is it really Numeric?

, 15 Nov 2005 92.1K 27
Rate this:
Please Sign up or sign in to vote.
A bad trap in the IsNumeric() function.

Introduction

This is not a true article, it is just a tip about a "trap" that I discovered in the VB.NET IsNumeric() function and that I need to share with the VB programmers community.

Background

If you used VB6 in the past especially, you would know that the IsNumeric() function is able to determine if a given string is convertible to a number or not. Of course, VB.NET also supports it, as it supports many other legacy VB functions (by referencing Microsoft.VisualBasic.dll). These functions should all be avoided in favor of the .NET Framework counterparts, however many VB programmers still use them. That's why I think it's important you are aware of this trap.

The trap

Let's start with this line of code:

MessageBox.Show(IsNumeric("12345").ToString())

Honestly, do you think it will ever return "False"? No, it always returns "True", doesn't it? Well, try it. Now:

  1. Close your application;
  2. Go to Control Panel / Regional and Language Options;
  3. On the "Regional Options" tab, click on the "Customize..." button;
  4. On the "Customize Regional Options" dialog box, change the "Decimal symbol" and the "Digit grouping symbol" in order to have them identical (for example, a comma in both cases);
  5. Confirm your new settings;
  6. Re-run your application: you'll get "False" as a result.

This behavior is due to the ambiguity you created with these settings in the parsing algorithm that tries to convert your string to a number. Other functions such as CDbl() will crash throwing a quite self-explaining exception in this situation, but not IsNumeric(), which simply gives a wrong result...

Let's not discuss about the logic of setting the "Decimal symbol" equal to the "Digit grouping symbol". And let's not discuss about the fact that a system dialog box allows you this kind of a setting! Also, let's not talk about the users' stupidity (yes, I found a user so stupid!). Let's talk about a workaround, to make your code "safe" in the IsNumeric() execution.

A possible workaround

A good workaround consists in explicitly setting the current thread culture, and - with the culture - programmatically setting the "Decimal symbol" and the "Digit grouping symbol", as in:

Imports System.Globalization
Imports System.Threading
...
Dim ci As New CultureInfo("it-IT")
Dim nfi As New NumberFormatInfo
nfi.CurrencyDecimalSeparator = ","
nfi.CurrencyGroupSeparator = "."
ci.NumberFormat = nfi
Thread.CurrentThread.CurrentCulture = ci

Of course, these settings you are "forcing" programmatically are limited to your application context and they won't influence (hopefully) anything else in the system.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Alberto Venditti
Technical Lead
Italy Italy
I was born in 1970.

My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

I graduated in Electronic Engineering and earned the following Microsoft certifications:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

I worked in IT as a developer, a teacher, a consultant, a technical writer, a technical leader.
IT knowledge applied to real life is my primary interest and focus.

Comments and Discussions

 
GeneralMy vote of 4 Pin
MarqW16-Feb-11 5:41
memberMarqW16-Feb-11 5:41 
GeneralAnother way to skin this kind of cat [modified] Pin
PeterBiddlecombe22-Nov-07 23:32
memberPeterBiddlecombe22-Nov-07 23:32 
The real problem here is that the system's settings are stupid - if comma is both decimal separator and digit grouping symbol, "100,123" is possibly just over a hundred, possibly just over a hundred thousand. So why worry about whether it's a number? If you cannot tell what a 'number' is supposed to be, you're in big trouble. So is any other application that deals with numbers. (And the result of IsNumeric is correct in its own way - an indeterminate number isn't really a number!)

I think you'd be much better off checking for silly settings like this when your app starts. If you find them, the app explains the silliness to the user firmly but politely, and then terminates. The worst that can happen is that an irate customer phones the support desk to complain, and eventually gets told of their own stupidity. You'll be doing them a service because most apps won't cope with the silly setting, and will either crash or start logging mysterious errors.
(Evidence: the SQL server v 2005 version of SQL Server Management studio was incapable of doing "Open table" on a table with numbers in it after I put this kind of setting into control panel.)

-- modified at 6:54 Friday 23rd November, 2007
GeneralRe: Another way to skin this kind of cat Pin
Alberto Venditti23-Nov-07 7:02
memberAlberto Venditti23-Nov-07 7:02 
QuestionHow to fix this IsNumberic's error in VB6.0? Pin
Tiger.xing14-Mar-06 21:03
memberTiger.xing14-Mar-06 21:03 
AnswerRe: How to fix this IsNumberic's error in VB6.0? Pin
Alberto Venditti23-Mar-06 22:10
memberAlberto Venditti23-Mar-06 22:10 
GeneralCouldnt you just... Pin
il_manti24-Nov-05 1:37
memberil_manti24-Nov-05 1:37 
GeneralRe: Couldnt you just... Pin
Alberto Venditti24-Nov-05 4:34
memberAlberto Venditti24-Nov-05 4:34 
GeneralRe: Couldnt you just... Pin
il_manti24-Nov-05 5:33
memberil_manti24-Nov-05 5:33 
GeneralRe: Couldnt you just... Pin
unklegwar28-Nov-05 7:07
memberunklegwar28-Nov-05 7:07 
GeneralRe: Couldnt you just... Pin
Alberto Venditti28-Nov-05 23:22
memberAlberto Venditti28-Nov-05 23:22 
QuestionAnd Regular Expression? Pin
Walter O Dias23-Nov-05 1:25
memberWalter O Dias23-Nov-05 1:25 
AnswerRe: And Regular Expression? Pin
Alberto Venditti23-Mar-06 22:13
memberAlberto Venditti23-Mar-06 22:13 
Generalalternative methods to IsNumeric, IsDate, InStr Pin
Alexis Rzewski22-Nov-05 3:31
memberAlexis Rzewski22-Nov-05 3:31 
GeneralRe: alternative methods to IsNumeric, IsDate, InStr Pin
Alberto Venditti23-Nov-05 0:59
memberAlberto Venditti23-Nov-05 0:59 
GeneralRe: alternative methods to IsNumeric, IsDate, InStr Pin
Alberto Venditti23-Nov-05 1:11
memberAlberto Venditti23-Nov-05 1:11 
GeneralNaive IsNumeric() inC# Pin
Guido_d16-Nov-05 2:57
memberGuido_d16-Nov-05 2:57 
GeneralRe: Naive IsNumeric() inC# Pin
Michael Potter16-Nov-05 4:38
memberMichael Potter16-Nov-05 4:38 
GeneralRe: Naive IsNumeric() inC# Pin
Jared James Sullivan26-Oct-07 10:36
memberJared James Sullivan26-Oct-07 10:36 
GeneralRe: Naive IsNumeric() inC# Pin
xfx19-Jan-09 22:53
memberxfx19-Jan-09 22:53 
GeneralRe: Naive IsNumeric() inC# Pin
nikoniko23-Nov-05 21:49
membernikoniko23-Nov-05 21:49 
GeneralRe: Naive IsNumeric() inC# Pin
percyboy19-Dec-05 15:13
memberpercyboy19-Dec-05 15:13 
GeneralImplement Visual Basic .NET IsNumeric Functionality by Using Visual C# .NET Pin
Stanimir Angeloff15-Nov-05 22:22
memberStanimir Angeloff15-Nov-05 22:22 
GeneralRe: Implement Visual Basic .NET IsNumeric Functionality by Using Visual C# .NET Pin
Alberto Venditti15-Nov-05 23:20
memberAlberto Venditti15-Nov-05 23:20 
GeneralBetter way IMO Pin
mav.northwind15-Nov-05 20:30
membermav.northwind15-Nov-05 20:30 
GeneralRe: Better way IMO Pin
Alberto Venditti15-Nov-05 23:16
memberAlberto Venditti15-Nov-05 23:16 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 15 Nov 2005
Article Copyright 2005 by Alberto Venditti
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid