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