Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 10th, 2004, 9:12 AM   #1
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I wrote a Python function a little while back that converts strings to numbers, for an app I was developing (which is currently on hold). It simply pulls out all the crap that isn't a number - for example, "8v52.x3" will become 852.3. It allows you to specify the return type - string, long integer or floating-point. Take a look:

def str2num (str, retType = ""):
  decimal = 0
  x = 0
  
  while x < len(str):
    if (not(str[x].isdigit() or str[x] == ".") or ((str[x] == ".") and decimal)):
      str = str[:x] + str[x+1:]
    elif str[x] == "." and not(decimal):
      decimal = 1
      x = x + 1
    else:
      x = x + 1
  
  if len(str) > 0:
    if str[0] == ".":
      str = "0" + str[:]
    if str[-1] == ".":
      str = str[:-1]
      decimal = 0
  
  if len(str) == 0:
    str = "0"
    
  if (retType == "string") or (retType == "str"):
    return str
  else:
    if retType == "float":
      return float(str)
    elif (retType == "int") or (retType == "integer"):
      return long(str)
    else:
      if decimal:
        return float(str)
      else:
        return long(str)
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 10th, 2004, 4:44 PM   #2
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 708
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
looks good, I'll have to check it out
thechristelegacy is offline   Reply With Quote
Old Dec 13th, 2004, 11:28 PM   #3
JoeUser
Newbie
 
Join Date: Dec 2004
Posts: 8
Rep Power: 0 JoeUser is on a distinguished road
Actually, Thanks! I was just looking for something to change strings to ints. It works like a charm! Now you can have the satisfaction of knowing somebody actually used your code for something! How often does that happen!?! (if you're not a pro-programmer, not often ^_^ )
JoeUser is offline   Reply With Quote
Old Dec 14th, 2004, 3:50 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Heh. It's nice to know.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 15th, 2004, 9:59 PM   #5
Beegie_B
Programmer
 
Join Date: Nov 2004
Location: Windsor, Ontario, Canada
Posts: 32
Rep Power: 0 Beegie_B is on a distinguished road
Send a message via ICQ to Beegie_B Send a message via MSN to Beegie_B
Solid and Simple Code Mate!

That is often quite useful, I made a small scrip that converted an IP addir to a simple int for my IP scanner, was almost the same code as your's

Beeg
Beegie_B is offline   Reply With Quote
Old Dec 16th, 2004, 5:19 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Heh. 'Twould be slightly different - different purposes, different code. Glad to hear it looks clean though.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC