[Tutor] ValueError: could not convert string to float: '13,2'

Peter Otten __peter__ at web.de
Sun Jan 19 09:22:13 CET 2014


Pierre Dagenais wrote:

> 
> 
> On 13-12-31 04:09 PM, Keith Winston wrote:
>> Hi PierreD, I think if you iterate over your strings with something like
>> this, it will do what you want, if I understand correctly (snum is your
>> string number, like "123,321"):
>> 
>> fnum = float(snum.replace(",", ".")
>> 
>> keith: rank beginner, take everything with a grain of salt!
>> 
>> 
>> 
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>> 
> Thank you all who responded, for now I'll go with Keith's solution and
> keep the locale for next time.
> As all the data has one digit after the decimal I can get away with
> myInt = int(temp.replace(',',''))
> to convert a string representation of a float to an integer. I'll just
> need to remember to divide everything by ten once the calculations are
> done. Not very elegant, but it'll work. I don't suppose there is a
> function for determining the number of digits after the decimal, is it?

It looks like you are trying to avoid rounding errors in decimal arithmetic. 
You might be interested in Python's decimal.Decimal type then.

> Also, anybody knows the maximum and/or minimum integer python will accept?

Integers in Python (*) are "unlimited"

>>> 10**1000 -1
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999                                  

In practice the amount of available memory will be the limit, but you are 
unlikely to reach that.

(*) Python 2 had int and long where values that could not represented by 
machine integers were automatically propagated to long. In Python 3 the int 
and long have been united as int.



More information about the Tutor mailing list