[Tutor] Help converting strings to numbers
Joerg Woelke
lumbricus@gmx.net
Tue, 28 Aug 2001 12:20:14 +0200
On Tue, Aug 28, 2001 at 11:36:25AM +0200, Charlie Clark wrote:
> Dear all,
>
> I'm getting a whole set of values and putting them in a dictionary like
> this
>
> {'preis': '2.029,00', 'marktpreis':'3.399,00'}
>
> >from which I need to generate a third price 'saving' which is ('preis' /
> 'marktpreis') * 100 but I don't know how to convert the string prices to
> floats to do the maths - it's an invalid literal.
>
preis='2.029,00'
import re
p=re.compile('^\d')
rp=re.sub(p,'',preis)
print rp
Hint:
Calculate in "Pfennige". So you avoid
floating point arithmetics with its problems.
int(rp)
> Please reply to me directly as I'm currently subscribed to the digest.
>
> Thanx
HTH, HAND
J"o! :-)
>
> Charlie
>
--
Huh?