Determine if a string is numeric: Difference between revisions

From Rosetta Code
Content added Content deleted
(Page creation)
 
No edit summary
Line 25: Line 25:
If (IsNumeric(Value)) {
If (IsNumeric(Value)) {
}
}

==[[PHP]]==

$string = '123';
if(is_numeric($string)) {
}

Revision as of 19:51, 21 January 2007

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)) {
       }

PHP

$string = '123';
if(is_numeric($string)) {
}