blob: 1a84b729d75b946f52686e0402e7b841083659ce [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
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
10The :mod:`fpformat` module defines functions for dealing with floating point
11numbers representations in 100% pure Python.
12
13.. note::
14
Georg Brandl4b491312007-08-31 09:22:56 +000015 This module is unnecessary: everything here can be done using the string
16 formatting functions described in the :ref:`string-formatting` section.
Georg Brandl116aa622007-08-15 14:28:22 +000017
18The :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
51Example::
52
53 >>> import fpformat
54 >>> fpformat.fix(1.23, 1)
55 '1.2'
56