Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a string to either int or float with priority on int?

Tags:

python

I couldn't find another answer when I wanted this, so I thought I would post my own solution for anyone else and also get corrections if I've done something wrong.

I had to make an automatic config file parser and I preferred to make numbers int if possible and float if not. The usual try/except conversion doesn't work by itself since any float will just be coerced to an int.

To clarify as someone asked, the case is basically to convert numbers as the person writing the config file data intended them to be. So anything with decimals would probably be intended to be a float. Also, I believe that float can be dangerous for some operators (e.g. ==, <, >) due to the nature of floating point numbers, and int will be converted to float when necessary. Therefore I prefer numbers to stay in int when possible. Not a big thing, just as a kind of convention for myself.

like image 203
KobeJohn Avatar asked Apr 09 '11 23:04

KobeJohn


People also ask

How do you convert a string to an int or a float?

In Python, you can convert a string str to an integer int and a floating point number float with int() and float() . This article describes the following contents. Use str() to convert an integer or floating point number to a string. You can also convert a list of strings to a list of numbers.

How do you convert a string to a float?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

How do you convert a string to an int number?

Use Integer.parseInt() to Convert a String to an Integer This method returns the string as a primitive type int. If the string does not contain a valid integer then it will throw a NumberFormatException.

How do you convert int to float?

To convert an integer data type to float you can wrap the integer with float64() or float32. Explanation: Firstly we declare a variable x of type int64 with a value of 5. Then we wrap x with float64(), which converts the integer 5 to float value of 5.00.


1 Answers

eval(s)

eval() is a function that accepts a string and processes it as Python code. When given any kind of literal as a string, it will return a Python object containing that value.

However, from a security standpoint, this is a bad idea, as someone could potentially feed instructions that crash or tamper with your program. You could require it to be a certain set of characters before processing, i.e.:

eval(s) if not set(s).difference('0123456789.') else None

This still probably isn't worth the headache, though. That said, you can get fancy, and allow expressions with calculations as well... and that might be worth trying!

num = lambda s: eval(s) if not set(s).difference('0123456789. *+-/e') else None

>>> num('10')
10
>>> num('10.0')
10.0
>>> num('1e3')
1000.0
>>> num('4.8 * 10**34')
4.8e+34
>>> num('1+2/3')
1.6666666666666665
like image 198
krs013 Avatar answered Sep 20 '22 16:09

krs013