s c h e m a t i c s : c o o k b o o k

/ NumberRecipes / Cookbook.NumberRecipesIsNumber

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Checking Whether a String Is a Valid Number

Problem

You need to check if a given string contains a valid number.

Solution

We supose that we want to check the next strings: "42", "2+3i", "3.1416", "2/3", "hello". First, we're going to try to convert that string to a number, and then we're going to pass the result to the predicate number? which is defined in Scheme. Let's see some examples:

(number? (string->number "42"))
(number? (string->number "2+3i"))
(number? (string->number "3.1416"))
(number? (string->number "2/3"))
(number? (string->number "hello"))

Discussion

There may also be times when you want to tell if a given number is an integer number a floating-point number. One solution is using the procedure string->number, and then we can use one of the next procedures:

(complex? (string->number "3+4i"))
(real? (string->number "#e1e10"))
(rational? (string->number "6/10"))
(integer? (string->number "3.0"))

Contributors

-- ChandraQuintas - 27 Apr 2004

CookbookForm
TopicType: Recipe
ParentTopic: NumberRecipes
TopicOrder: 010

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest