7 examples of 'str2num python' in Python

Every line of 'str2num python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
13def is_num(s):
14 try:
15 int(s)
16 except ValueError:
17 return False
18 else:
19 return True
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

198def num2str(num):
199 # TODO: i18n...
200 # TODO: this is *UGLY*
201 # TODO: OMG, i'm *so* embarrassed
202 # TODO: but it works... sort of.
203 if num == 0:
204 return '-'
205 s = list(reversed(str(num)))
206 for idx in reversed(range(3, len(s), 3)):
207 s.insert(idx, ',')
208 return ''.join(reversed(s))
16def _handle_int_part(num,eng_str):
17 assert num > 0 and num < 1000
18
19 baiwei = num / 100
20 shiwei = num / 10 % 10
21 gewei = num % 10
22
23 if baiwei != 0:
24 eng_str += DIGIT_TABLE[baiwei] + CARDINAL_NUMBER[100]
25 if shiwei > 0 or gewei > 0:
26 eng_str += CONJUNCTION
27 if shiwei == 0:
28 eng_str += DIGIT_TABLE[gewei]
29 elif shiwei == 1:
30 eng_str += DIGIT_TABLE[shiwei*10+gewei]
31 else:
32 if gewei != 0:
33 eng_str += TENS_TABLE[shiwei*10] + DIGIT_TABLE[gewei]
34 else:
35 eng_str += TENS_TABLE[shiwei*10]
36
37 return eng_str
14def toint(num):
15 if num < 0:
16 return 0
17 elif num > 65535:
18 return 65535
19 else:
20 return int(num)
156def get_numbers(num):
157 card_and_port = str(num)
158 card_and_port = re.split('/|-',card_and_port)
159 card = card_and_port[0]
160 fromPort = card_and_port[1]
161 toPort = fromPort if len(card_and_port) <= 2 else card_and_port[2]
162 return card, fromPort, toPort
30def tryNum(s):
31 if s.startswith('0x'):
32 try:
33 return int(s, 16)
34 except ValueError:
35 pass
36 try:
37 return int(s)
38 except ValueError:
39 pass
40 if sys.version_info[0] < 3:
41 try:
42 return long(s)
43 except ValueError:
44 pass
45 return s
3def num(ip):
4 if type(ip) in (int, float):
5 return ip
6 elif type(ip) != str:
7 raise TypeError('not a valid input')
8
9 try:
10 return int(ip)
11 except ValueError:
12 try:
13 return float(ip)
14 except ValueError:
15 raise ValueError('could not convert string to int or float')

Related snippets