String to Float, without introducing errors

Thomas Passin list1 at tompassin.net
Sat Dec 17 14:09:45 EST 2022


On 12/17/2022 1:41 PM, Mats Wichmann wrote:
> On 12/17/22 07:15, Thomas Passin wrote:
>> You have strings, and you want to end up with numbers.  The numbers 
>> are not integers.  Other responders have gone directly to whether you 
>> should use float or decimal as the conversion, but that is a secondary 
>> matter.
>>
>> If you have integers, convert with
>>
>> integer = int(number_string)
>>> -64550.727
> 
> they pretty clearly aren't integers:

Of course they aren't.  That's why I gave the line with float() too. 
It's useful to see that there is a basic pattern here:  Given a string, 
expect to need to convert it to what you actually want, int, float, 
decimal, whatever.

>>> -64511.489
>>> -64393.637
>>> -64196.763
>>> -63920.2
>>> -63563.037
>>> -63124.156
>>> -62602.254
>>> -61995.895
>>> -61303.548
>>> -60523.651
>>> -59654.66



More information about the Python-list mailing list