Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`fpformat` --- Floating point conversions |
| 3 | ============================================== |
| 4 | |
| 5 | .. module:: fpformat |
| 6 | :synopsis: General floating point formatting functions. |
Brett Cannon | fe59851 | 2008-05-10 22:11:45 +0000 | [diff] [blame] | 7 | :deprecated: |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 8 | |
Brett Cannon | fe59851 | 2008-05-10 22:11:45 +0000 | [diff] [blame] | 9 | .. deprecated:: 2.6 |
Brett Cannon | a975cd4 | 2008-05-11 01:06:54 +0000 | [diff] [blame] | 10 | The :mod:`fpformat` module has been removed in Python 3.0. |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 11 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 12 | .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il> |
| 13 | |
| 14 | |
| 15 | The :mod:`fpformat` module defines functions for dealing with floating point |
| 16 | numbers representations in 100% pure Python. |
| 17 | |
| 18 | .. note:: |
| 19 | |
Mark Summerfield | 7f626f4 | 2007-08-30 15:03:03 +0000 | [diff] [blame] | 20 | This module is unnecessary: everything here can be done using the ``%`` string |
| 21 | interpolation operator described in the :ref:`string-formatting` section. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 22 | |
| 23 | The :mod:`fpformat` module defines the following functions and an exception: |
| 24 | |
| 25 | |
| 26 | .. function:: fix(x, digs) |
| 27 | |
| 28 | Format *x* as ``[-]ddd.ddd`` with *digs* digits after the point and at least one |
| 29 | digit before. If ``digs <= 0``, the decimal point is suppressed. |
| 30 | |
| 31 | *x* can be either a number or a string that looks like one. *digs* is an |
| 32 | integer. |
| 33 | |
| 34 | Return value is a string. |
| 35 | |
| 36 | |
| 37 | .. function:: sci(x, digs) |
| 38 | |
| 39 | Format *x* as ``[-]d.dddE[+-]ddd`` with *digs* digits after the point and |
| 40 | exactly one digit before. If ``digs <= 0``, one digit is kept and the point is |
| 41 | suppressed. |
| 42 | |
| 43 | *x* can be either a real number, or a string that looks like one. *digs* is an |
| 44 | integer. |
| 45 | |
| 46 | Return value is a string. |
| 47 | |
| 48 | |
| 49 | .. exception:: NotANumber |
| 50 | |
| 51 | Exception raised when a string passed to :func:`fix` or :func:`sci` as the *x* |
| 52 | parameter does not look like a number. This is a subclass of :exc:`ValueError` |
| 53 | when the standard exceptions are strings. The exception value is the improperly |
| 54 | formatted string that caused the exception to be raised. |
| 55 | |
| 56 | Example:: |
| 57 | |
| 58 | >>> import fpformat |
| 59 | >>> fpformat.fix(1.23, 1) |
| 60 | '1.2' |
| 61 | |