| 1 | \section{\module{fpformat} ---
|
|---|
| 2 | Floating point conversions}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{fpformat}
|
|---|
| 5 | \sectionauthor{Moshe Zadka}{[email protected]}
|
|---|
| 6 | \modulesynopsis{General floating point formatting functions.}
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 | The \module{fpformat} module defines functions for dealing with
|
|---|
| 10 | floating point numbers representations in 100\% pure
|
|---|
| 11 | Python. \note{This module is unneeded: everything here could
|
|---|
| 12 | be done via the \code{\%} string interpolation operator.}
|
|---|
| 13 |
|
|---|
| 14 | The \module{fpformat} module defines the following functions and an
|
|---|
| 15 | exception:
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | \begin{funcdesc}{fix}{x, digs}
|
|---|
| 19 | Format \var{x} as \code{[-]ddd.ddd} with \var{digs} digits after the
|
|---|
| 20 | point and at least one digit before.
|
|---|
| 21 | If \code{\var{digs} <= 0}, the decimal point is suppressed.
|
|---|
| 22 |
|
|---|
| 23 | \var{x} can be either a number or a string that looks like
|
|---|
| 24 | one. \var{digs} is an integer.
|
|---|
| 25 |
|
|---|
| 26 | Return value is a string.
|
|---|
| 27 | \end{funcdesc}
|
|---|
| 28 |
|
|---|
| 29 | \begin{funcdesc}{sci}{x, digs}
|
|---|
| 30 | Format \var{x} as \code{[-]d.dddE[+-]ddd} with \var{digs} digits after the
|
|---|
| 31 | point and exactly one digit before.
|
|---|
| 32 | If \code{\var{digs} <= 0}, one digit is kept and the point is suppressed.
|
|---|
| 33 |
|
|---|
| 34 | \var{x} can be either a real number, or a string that looks like
|
|---|
| 35 | one. \var{digs} is an integer.
|
|---|
| 36 |
|
|---|
| 37 | Return value is a string.
|
|---|
| 38 | \end{funcdesc}
|
|---|
| 39 |
|
|---|
| 40 | \begin{excdesc}{NotANumber}
|
|---|
| 41 | Exception raised when a string passed to \function{fix()} or
|
|---|
| 42 | \function{sci()} as the \var{x} parameter does not look like a number.
|
|---|
| 43 | This is a subclass of \exception{ValueError} when the standard
|
|---|
| 44 | exceptions are strings. The exception value is the improperly
|
|---|
| 45 | formatted string that caused the exception to be raised.
|
|---|
| 46 | \end{excdesc}
|
|---|
| 47 |
|
|---|
| 48 | Example:
|
|---|
| 49 |
|
|---|
| 50 | \begin{verbatim}
|
|---|
| 51 | >>> import fpformat
|
|---|
| 52 | >>> fpformat.fix(1.23, 1)
|
|---|
| 53 | '1.2'
|
|---|
| 54 | \end{verbatim}
|
|---|