[Home] Module Microsoft.FSharp.MLLib.Printf


Extensible printf-style formatting for numbers and other datatypes

Format specifications are strings with "%" markers indicating format placeholders. Format placeholders consist of:

    %[flags][width][.precision][type]
 

where the type is interpreted as follows: (note int = int32 for F#)

    %b:         bool, formatted as "true" or "false"
    %s:         string, formatted as its unescaped contents
    %d, %i:     int/int32, formatted as a signed decimal integer
    %u:         int/int32, formatted as an unsigned decimal integer
    %x, %X, %o: int/int32, formatted as an unsigned hexadecimal 
                (a-f)/Hexadecimal (A-F)/Octal integer
    %ld, %li, %lu, %lx, %lX, %lo: same, but an int64
    %nd, %ni, %nu, %nx, %nX, %no: same, but a nativeint
    %Ud, %Ui, %Uu, %Ux, %UX, %Uo: same, but an unsigned int32 (uint32)
    %Uld, %Uli, %Ulu, %Ulx, %UlX, %Ulo: same, but an unsigned int64 (uint64)
    %Und, %Uni, %Unu, %Unx, %UnX, %Uno: same, but an unsigned nativeint 
                                        (unativeint)
 
    %e, %E, %f, %g, %G: C-style floating point format specifications, i.e
 
          %e, %E: Signed value having the form [-]d.dddde[sign]ddd where 
                  d is a single decimal digit, dddd is one or more decimal
                  digits, ddd is exactly three decimal digits, and sign 
                  is + or -
 
          %f:     Signed value having the form [-]dddd.dddd, where dddd is one
                  or more decimal digits. The number of digits before the 
                  decimal point depends on the magnitude of the number, and 
                  the number of digits after the decimal point depends on 
                  the requested precision.
 
          %g, %G: Signed value printed in f or e format, whichever is 
                  more compact for the given value and precision.
 
 
    %M:     System.Decimal value
 
    %O:     Any value, printed by boxing the object and using it's ToString method(s)
 
    %a: a general format specifier, requires two arguments:
          (1) a function which accepts two arguments:
                (a) a context parameter of the appropriate type for the
                    given formatting function (e.g. an out_channel)
                (b) a value to print
               and which either outputs or returns appropriate text.
 
          (2) the particular value to print
 
 
    %t: a general format specifier, requires one argument:
          (1) a function which accepts a context parameter of the
              appropriate type for the given formatting function (e.g. 
              an out_channel)and which either outputs or returns 
              appropriate text.
 
 

Valid flags are:

     0: add zeros instead of spaces to make up the required width
     '-': left justify the result within the width specified
     '+': add a '+' character if the number is positive (to match a '-' sign 
          for negatives)
     ' ': add an extra space if the number is positive (to match a '-' 
              sign for negatives)
 

The printf '#' flag is not supported in this release and a compile-time error will be reported if it is used.

   Printf.printf "Hello %s, %d = %d * %d" "World" 6 2 3;
   Printf.sprintf "Hello %s" "World";
   Printf.printf "Hello %a" output_string "World";
 

Types

TypeDescription
type bstring_format The type of formats accepted by printers that produce strings but print intermediate results to buffers Note: an abbreviation for ('a,string) kbuffer_format
type buffer_format The type of formats accepted by printers that write to buffers Note: an abbreviation for ('a,unit) kbuffer_format
type channel_format The type of formats accepted by printers that write to channels Note: an abbreviation for ('a,unit) kchannel_format
type kbuffer_format The type of formats accepted by print-then-call printers that write to buffers Note: an abbreviation for ('a,t,unit,'d) format4
type kchannel_format The type of formats accepted by print-then-call printers that write to channels Note: an abbreviation for ('a,out_channel,unit,'d) format4
type kstreamwriter_format The type of formats accepted by print-then-call printers that write to .NET stream writers Note: an abbreviation for ('a,StreamWriter,unit,'d) format4
type kstring_format The type of formats accepted by print-then-call printers that produce strings but print intermediate results to buffers The type of formats accepted by print-then-call printers that produce strings Note: an abbreviation for ('a,unit,string,'d) format4
type streamwriter_format The type of formats accepted by printers that write to .NET stream writers Note: an abbreviation for ('a,unit) kstreamwriter_format
type string_format The type of formats accepted by printers that produce strings Note: an abbreviation for ('a,string) kstring_format

Values

ValueDescription
val bfailwithf : ('a,t,unit,'d) format4 -> 'a
Print to a string buffer and raise an exception with the given result. Helper printers must print to string buffers.
val bfprintf : out_channel -> 'a buffer_format -> 'a
Print to an F# channel, but intermediary printers write via a buffer. Useful if your primitive printing functions are defined to write to buffers, but you ultimately want to go to a file.
val bprintf : t -> 'a buffer_format -> 'a
Print to a string buffer, which is a System.Text.StringBuilder
val bsprintf : 'a bstring_format -> 'a
Print to a string buffer, which is a System.Text.StringBuilder. Return the result as a string. Helper printers must print to string buffers.
val eprintf : 'a channel_format -> 'a
Formatted printing to stderr
val failwithf : ('a,unit,string,'d) format4 -> 'a
Print to a string buffer and raise an exception with the given result. Helper printers must return strings.
val fprintf : out_channel -> 'a channel_format -> 'a
Print to an F# channel
val kbprintf : (unit -> 'd) -> t -> ('a,'d) kbuffer_format -> 'a
bprintf, but call the given 'final' function to generate the result. See [[kprintf]].
val kbsprintf : (string -> 'd) -> ('a,'d) kbuffer_format -> 'a
bsprintf, but call the given 'final' function to generate the result. See [[kprintf]].
val kfprintf : (unit -> 'd) -> out_channel -> ('a,'d) kchannel_format -> 'a
fprintf, but call the given 'final' function to generate the result. See [[kprintf]].
val kprintf : (string -> 'd) -> ('a,'d) kstring_format -> 'a
printf, but call the given 'final' function to generate the result. For example, these let the printing force a flush after all output has been entered onto the channel, but not before.
val ksprintf : (string -> 'd) -> ('a,'d) kstring_format -> 'a
sprintf, but call the given 'final' function to generate the result. See [[kprintf]].
val kswprintf :
  (unit -> 'd) -> StreamWriter -> ('a,'d) kstreamwriter_format -> 'a
fprintf, but call the given 'final' function to generate the result. See [[kprintf]].
val printf : 'a channel_format -> 'a
Formatted printing to stdout
val sprintf : 'a string_format -> 'a
Print to a string via an internal string buffer and return the result as a string. Helper printers must return strings.
val swprintf : StreamWriter -> 'a streamwriter_format -> 'a
Print to a .NET StreamWriter

See Also

Microsoft.FSharp.MLLib


Documentation for assembly mllib, version 1.1.10.1, generated using F# version 1.1.10.1