How to format numbers

To format a number you can use the function format_number(). It will return a string. The first parameter is the number you want to format, the second is a boolean value whether or not you want a thousands separator, the third parameter shows the number of digits, and with the last you can set the target locale.

Example 5.9. Formatting a number

horst@horstnotebook:~> python
Python 2.3.2 (#1, Oct 28 2003, 21:22:16)
[GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from hk_classes import *
>>> number=12345.6789
>>> print number
12345.6789
>>> print format_number(number,1,2)
12.345,68
>>> print format_number(number,True,2)
12.345,68
>>> print format_number(number,True,2,"C")
12345.68
>>> print format_number(number,True,2,"de_DE")
12.345,68
>>> print format_number(number,False,2,"de_DE")
12345,68
>>>