[Tutor] how do you format/fix/convert types of numbers when you print them?

Denis spirou@aragne.com
Mon, 28 Aug 2000 01:52:07 +0200


Le Sun, Aug 27, 2000 at 06:09:25PM -0500, Charles Gruschow, Jr. pianota:
> how do you format/fix/convert types of numbers when you print them?
> 
> how do you convert one number type to another?
> 
> how do you print only a certain number of decimal places?


look at this interactive session log, check the docs for
further information.

>>> a = 112.25689
>>> print a
112.25689
>>> print type(a)
<type 'float'>
>>> b = int(a)
>>> print b
112
>>> print type(b)
<type 'int'>
>>> c = float(b)
>>> print c
112.0
>>> print type(c)
<type 'float'>
>>> print "%d" % b
112
>>> print "%04d" % b
0112
>>> print "%4d" % b
 112
>>> print "%4d" % a
 112
>>> print "%.2f" % a
112.26
  
Play with the interpreter, that one of the pleasures of Python. ;-)


-- 
Denis FRERE
P3B    : Club Free-Pytho-Linuxien Caroloregien http://www.p3b.org
Aragne : Internet - Reseaux - Formations  http://www.aragne.com