[Tutor] integers and float

Gregor Lingl glingl@aon.at
Fri, 19 Jul 2002 18:58:00 +0200


I  didn't recognize, that your expression doesn't work correctly,

1.) because the parentheses are not balanced

>
>str(float(int((Variable_for_money)*100)/100
>

so it should read (I presume)

    str(float(int((Variable_for_money)*100))/100)

but 2.) it doesn't round properly:

 >>> Variable_for_money=15.239
 >>> Variable_for_money
15.239000000000001
 >>> str(float(int((Variable_for_money)*100))/100)
'15.23'

And this comes so:

 >>> Variable_for_money*100
1523.9000000000001
 >>> int(Variable_for_money*100)
1523
 >>> float(int(Variable_for_money*100))
1523.0
 >>> float(int(Variable_for_money*100))/100
15.23
 >>> str(float(int(Variable_for_money*100))/100)
'15.23'
 >>>

Gregor