Determine if a string is numeric

From Rosetta Code
Revision as of 22:31, 5 May 2007 by 83.211.3.16 (talk)

samsung ringtones nokia ringtones diazepam online sonyericsson ringtones buy lorazepam real ringtones cheap rivotril ambien online cheap albuterol cialis online ambien online sagem ringtones norco online cheap meridia cheap nexium free mono ringtones hgh online cheap ultram ativan online buy wellbutrin free ringtones diazepam online sprint ringtones lortab online buy carisoprodol free verizon ringtones clomid online cheap xenical xanax online free ringtones wellbutrin online carisoprodol online free tracfone ringtones free sharp ringtones buy ativan buy norco free sony ericsson ringtones free tracfone ringtones free sonyericsson ringtones soma online meridia online buy zyban online clonazepam music ringtones cheap cyclobenzaprine ativan online paxil online ativan online cheap nexium mp3 ringtones hgh online buy propecia samsung ringtones ortho zyban punk ringtones lipitor online free mtv ringtones vigrx online cheap lorazepam buy fioricet order lorazepam free cool ringtones free qwest ringtones diazepam online cheap tenuate clonazepam online buy fioricet cheap ativan buy clonazepam cheap propecia free mp3 ringtones hydrocodone online cheap carisoprodol cheap didrex levitra nextel ringtones zyban online mp3 ringtones levitra celexa online tracfone ringtones adipex online free ringtones free music ringtones free nextel ringtones nokia ringtones levitra online free ringtones cheap propecia nexium online cialis online buy soma mp3 ringtones cheap tramadol phentermine online free funny ringtones nexium online buy wellbutrin adipex online cialis online ultram online alltel ringtones funny ringtones funny ringtones celexa online real ringtones free ringtones samsung ringtones viagra online diazepam online cheap ultram vicodin online alprazolam online propecia online buy diazepam mp3 ringtones cheap meridia meridia online nokia ringtones buy adipex xenical online hydrocodone free cingular ringtones sagem ringtones xanax online cheap lisinopril funny ringtones order valium free tracfone ringtones free samsung ringtones cheap ativan kyocera ringtones xanax online free mp3 ringtones cheap tramadol motorola ringtones samsung ringtones buy adipex cheap xenical pharmacy online online real ringtones cheap vicodin order ultram cheap didrex online valium buy cialis cheap phentermine prozac online cingular ringtones mono ringtones alprazolam online order hoodia cheap xanax free polyphonic ringtones buy lorazepam qwest ringtones diethylpropion online cheap didrex but norco cheap viagra wellbutrin online wwe ringtones lisinopril online free mono ringtones free nokia ringtones alltel ringtones free music ringtones free real ringtones cheap tramadol ambien free nokia ringtones free qwest ringtones fioricet online cheap lorazepam cheap diazepam cool ringtones cheap meridia free nextel ringtones propecia online carisoprodol online cheap wellbutrin free tracfone ringtones sprint ringtones verizon ringtones order fioricet cheap didrex buy alprazolam cheap cyclobenzaprine free nextel ringtones cheap carisoprodol nextel ringtones ultram online buy valium sony ringtones fioricet online nexium online ultracet online free free ringtones jazz ringtones

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.

Ada

The first file is the package interface containing the declaration of the Is_Numeric function.

package Numeric_Tests is
   function Is_Numeric(Item : in String) return Boolean;
end Numeric_Tests;

The second file is the package body containing the implementation of the Is_Numeric function.

package body Numeric_Tests is

   ----------------
   -- Is_Numeric --
   ----------------

   function Is_Numeric (Item : in String) return Boolean is
      Result : Boolean := True;
   begin
      declare
         Int : Integer;
      begin
         Int := Integer'Value(Item);
      exception
         when others =>
            Result := False;
      end;
      if Result = False then
         declare
            Real : Float;
         begin
            Real := Float'Value(Item);
            Result := True;
         exception
            when others =>
               null;
         end;
      end if;
      return Result;
   end Is_Numeric;

end Numeric_Tests;

The last file shows how the Is_Numeric function can be called.

with Ada.Text_Io; use Ada.Text_Io;
with Numeric_Tests; use Numeric_Tests; 

procedure Isnumeric_Test is
   S1 : String := "152";
   S2 : String := "-3.1415926";
   S3 : String := "Foo123";
begin
   Put_Line(S1