Determine if a string is numeric

From Rosetta Code
Revision as of 19:47, 21 January 2007 by rosettacode>Created by: X (Page creation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Determine if a string is numeric
You are encouraged to solve this task according to the task description, using any language you may know.

Demonstrates how to implement a custom IsNumeric method in other .NET/Mono languages that do not wish to reference the Microsoft.VisualBasic.dll assembly.

VB.NET

Compiler: Microsoft (R) Visual Basic Compiler version 8.0

Dim Value As String = "123"
If IsNumeric(Value) Then
    
End If

C#

Compiler: Microsoft (R) Visual C# 2005 Compiler version 8.00

       public static bool IsNumeric(string value)
       {
           value = value.Trim();
           if (value == string.Empty) return false;
           if (char.IsSymbol(value[0]) == false && char.IsNumber(value[0]) == false) return false;
           if (char.IsSymbol(value[0]) && value.Length > 1 && char.IsNumber(value[1]) == false) return false;
           return true;
       }
       
       string Value = "123";
       If (IsNumeric(Value)) {
       }