Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > We have string.isdigit(), why not string.isNumber()?

Reply
Thread Tools

We have string.isdigit(), why not string.isNumber()?

 
 
MooMaster
Guest
Posts: n/a
 
      05-01-2008
N00b question alert! I did a search for isdigit() in the group
discussion, and it didn't look like the question had been asked in the
first 2 pages, so sorry if it was...

The manual documentation says:
"isdigit( )

Return true if all characters in the string are digits and there is at
least one character, false otherwise.
For 8-bit strings, this method is locale-dependent. "

So it makes sense that something like 5.6 would return false. But what
if we want to make sure that our string is a valid number, ie decimals
included?

I know how to write a regexp or method or whatever to do this, my main
question is *why* something like an isNumber() method is not baked
into the class. Does such functionality exist somewhere else in the
standard library that I'm just missing?

 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      05-01-2008
In article
<(E-Mail Removed)>,
MooMaster <(E-Mail Removed)> wrote:

> So it makes sense that something like 5.6 would return false. But what
> if we want to make sure that our string is a valid number, ie decimals
> included?


Just call int(x) or float(x) inside a try block and see if if it raises an
exception.
 
Reply With Quote
 
 
 
 
Dan Bishop
Guest
Posts: n/a
 
      05-01-2008
On Apr 30, 7:56 pm, MooMaster <(E-Mail Removed)> wrote:
> N00b question alert! I did a search for isdigit() in the group
> discussion, and it didn't look like the question had been asked in the
> first 2 pages, so sorry if it was...
>
> The manual documentation says:
> "isdigit( )
>
> Return true if all characters in the string are digits and there is at
> least one character, false otherwise.
> For 8-bit strings, this method is locale-dependent. "
>
> So it makes sense that something like 5.6 would return false. But what
> if we want to make sure that our string is a valid number, ie decimals
> included?
>
> I know how to write a regexp or method or whatever to do this, my main
> question is *why* something like an isNumber() method is not baked
> into the class. Does such functionality exist somewhere else in the
> standard library that I'm just missing?


A string s is a valid number if float(s) does not raise a ValueError.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why :: ? Why not : ? Why not . ? <- less clutter ?!? Skybuck Flying C++ 16 08-25-2007 09:48 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why I have error message "not have an entry point defined"? =?Utf-8?B?ZGF2aWQ=?= ASP .Net 6 08-18-2005 11:09 AM



Advertisments