···11+---
22+title: Floating-point cheat sheet for Python
33+description: Tips for using floating-point and decimal numbers in Python
44+---
55+66+Floating-Point Types
77+--------
88+Almost all platforms map Python floats to [IEEE 754](/formats/fp/)
99+double precision.
1010+1111+ f = 0.1
1212+1313+Decimal Types
1414+-------------
1515+Python has an [arbitrary-precision](/formats/exact/) decimal type named <code>Decimal</code> in the <code>decimal</code> module, which
1616+also allows to choose the [rounding mode](/errors/rounding/).
1717+ a = Decimal('0.1')
1818+ b = Decimal('0.2')
1919+ c = a + b # returns a Decimal representing exactly 0.3
2020+2121+2222+How to Round
2323+------------
2424+To get a string:
2525+ "%.2f" % 1.2399 # returns "1.24"
2626+ "%.3f" % 1.2399 # returns "1.240"
2727+ "%.2f" % 1.2 # returns "1.20"
2828+To print to standard output:
2929+ print "%.2f" % 1.2399 # just use print and string formatting
3030+If you need a specific [rounding mode](/errors/rounding/) and other parameters can be defined in a Context object:
3131+ getcontext().prec = 7
3232+3333+Resources
3434+---------
3535+* [Floating Point Arithmetic: Issues and Limitations](http://docs.python.org/tutorial/floatingpoint.html)
3636+* [The decimal module](http://docs.python.org/library/decimal.html)
3737+ * [Context objects](http://docs.python.org/library/decimal.html#context-objects)
3838+* [String formatting in Python](http://docs.python.org/library/stdtypes.html#string-formatting-operations)