base-4.6.0.1: Basic libraries

Portability | portable
Stability | provisional
Maintainer | lennart@augustsson.net
Safe Haskell | Safe

Text.Printf

Description

A C printf like formatter.

Synopsis

- printf :: PrintfType r => String -> r
- hPrintf :: HPrintfType r => Handle -> String -> r
- class PrintfType t
- class HPrintfType t
- class PrintfArg a
- class IsChar c

# Documentation

printf :: PrintfType r => String -> r

Format a variable number of arguments with the C-style formatting string. The return value is either `String` or `(`IO` a)`.

The format string consists of ordinary characters and /conversion specifications/, which specify how to format one of the arguments to printf in the output string. A conversion specification begins with the character `%`, followed by one or more of the following flags:

        -      left adjust (default is right adjust)
        +      always use a sign (+ or -) for signed conversions
        0      pad with zeroes rather than spaces

followed optionally by a field width:

        num    field width
        *      as num, but taken from argument list

followed optionally by a precision:

        .num   precision (number of decimal places)

and finally, a format character:

        c      character               Char, Int, Integer, ...
        d      decimal                 Char, Int, Integer, ...
        o      octal                   Char, Int, Integer, ...
        x      hexadecimal             Char, Int, Integer, ...
        X      hexadecimal             Char, Int, Integer, ...
        u      unsigned decimal        Char, Int, Integer, ...
        f      floating point          Float, Double
        g      general format float    Float, Double
        G      general format float    Float, Double
        e      exponent format float   Float, Double
        E      exponent format float   Float, Double
        s      string                  String

Mismatch between the argument types and the format string will cause an exception to be thrown at runtime.

Examples:

       > printf "%d\n" (23::Int)
       23
       > printf "%s %s\n" "Hello" "World"
       Hello World
       > printf "%.2f\n" pi
       3.14

hPrintf :: HPrintfType r => Handle -> String -> r

Similar to `printf`, except that output is via the specified `Handle`. The return type is restricted to `(`IO` a)`.

class PrintfType t

The `PrintfType` class provides the variable argument magic for `printf`. Its implementation is intentionally not visible from this module. If you attempt to pass an argument of a type which is not an instance of this class to `printf` or `hPrintf`, then the compiler will report it as a missing instance of `PrintfArg`.

Instances

IsChar c => PrintfType [c] |
PrintfType (IO a) |
(PrintfArg a, PrintfType r) => PrintfType (a -> r) |

class HPrintfType t

The `HPrintfType` class provides the variable argument magic for `hPrintf`. Its implementation is intentionally not visible from this module.

Instances

HPrintfType (IO a) |
(PrintfArg a, HPrintfType r) => HPrintfType (a -> r) |

class PrintfArg a

Instances

PrintfArg Char |
PrintfArg Double |
PrintfArg Float |
PrintfArg Int |
PrintfArg Int8 |
PrintfArg Int16 |
PrintfArg Int32 |
PrintfArg Int64 |
PrintfArg Integer |
PrintfArg Word |
PrintfArg Word8 |
PrintfArg Word16 |
PrintfArg Word32 |
PrintfArg Word64 |
IsChar c => PrintfArg [c] |

class IsChar c

Instances

IsChar Char |