FSharp.Core


Printf Module

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].

Types

Type Description

BuilderFormat<'T, 'Result>

Represents a statically-analyzed format associated with writing to a StringBuilder. The first type parameter indicates the arguments of the format operation and the last the overall return type.

BuilderFormat<'T>

Represents a statically-analyzed format associated with writing to a StringBuilder. The type parameter indicates the arguments and return type of the format operation.

StringFormat<'T, 'Result>

Represents a statically-analyzed format when formatting builds a string. The first type parameter indicates the arguments of the format operation and the last the overall return type.

StringFormat<'T>

Represents a statically-analyzed format when formatting builds a string. The type parameter indicates the arguments and return type of the format operation.

TextWriterFormat<'T, 'Result>

Represents a statically-analyzed format associated with writing to a TextWriter. The first type parameter indicates the arguments of the format operation and the last the overall return type.

TextWriterFormat<'T>

Represents a statically-analyzed format associated with writing to a TextWriter. The type parameter indicates the arguments and return type of the format operation.

Functions and values

Function or value Description

bprintf builder format

Full Usage: bprintf builder format

Parameters:
Returns: 'T The return type and arguments of the formatter.

Print to a StringBuilder

builder : StringBuilder

The StringBuilder to print to.

format : BuilderFormat<'T>

The input format or interpolated string.

Returns: 'T

The return type and arguments of the formatter.

Example

Using interpolated strings:

 open Printf
 open System.Text

 let buffer = new StringBuilder()

 bprintf buffer $"Write three = {1+2}" 
 buffer.ToString()
Evaluates to "Write three = 3".

Example

Using % format patterns:

 open Printf
 open System.Text

 let buffer = new StringBuilder()

 bprintf buffer "Write five = %d" (3+2)
 buffer.ToString()
Evaluates to "Write five = 5".

eprintf format

Full Usage: eprintf format

Parameters:
Returns: 'T The return type and arguments of the formatter.

Formatted printing to stderr

format : TextWriterFormat<'T>

The input formatter.

Returns: 'T

The return type and arguments of the formatter.

Example

Using interpolated strings:

 eprintf $"Write three = {1+2}"
After evaluation the text "Write three = 3" is written to stderr.

Example

Using % format patterns:

 eprintf "Write five = %d" (3+2)
After evaluation the text "Write five = 5" is written to stderr.

eprintfn format

Full Usage: eprintfn format

Parameters:
Returns: 'T The return type and arguments of the formatter.

Formatted printing to stderr, adding a newline

format : TextWriterFormat<'T>

The input formatter.

Returns: 'T

The return type and arguments of the formatter.

Example

Using interpolated strings:

 eprintfn $"Write three = {1+2}" 
 eprintfn $"Write four = {2+2}"
After evaluation two lines are written to stderr.

Example

Using % format patterns:

 eprintfn "Write five = %d" (3+2)
 eprintfn "Write six = %d" (3+3)
After evaluation two lines are written to stderr.

failwithf format

Full Usage: failwithf format

Parameters:
Returns: 'T The arguments of the formatter.

Print to a string buffer and raise an exception with the given result. Helper printers must return strings.

format : StringFormat<'T, 'Result>

The input formatter.

Returns: 'T

The arguments of the formatter.

Example

 failwithf "That's wrong. Five = %d and six = %d" (3+2) (3+3)
Throws Exception with message "That's wrong. Five = 5 and six = 6".

fprintf textWriter format

Full Usage: fprintf textWriter format

Parameters:
Returns: 'T The return type and arguments of the formatter.

Print to a text writer.

textWriter : TextWriter

The TextWriter to print to.

format : TextWriterFormat<'T>

The input formatter.

Returns: 'T

The return type and arguments of the formatter.

Example

Using interpolated strings:

 open Printf
 open System.IO

 let file = File.CreateText("out.txt")

 fprintf file $"Write three = {1+2}" 
 file.Close()
After evaluation the file contains the text "Write three = 3".

Example

Using % format patterns:

 open Printf
 open System.IO

 let file = File.CreateText("out.txt")

 fprintf file "Write five = %d" (3+2)
 file.Close()
After evaluation the file contains the text "Write five = 5".

fprintfn textWriter format

Full Usage: fprintfn textWriter format

Parameters:
Returns: 'T The return type and arguments of the formatter.

Print to a text writer, adding a newline

textWriter : TextWriter

The TextWriter to print to.

format : TextWriterFormat<'T>

The input formatter.

Returns: 'T

The return type and arguments of the formatter.

Example

Using interpolated strings:

 open Printf
 open System.IO

 let file = File.CreateText("out.txt")

 fprintfn file $"Write three = {1+2}" 
 fprintfn file $"Write four = {2+2}" 
 file.Close()
After evaluation the file contains two lines.

Example

Using % format patterns:

 open Printf
 open System.IO

 let file = File.CreateText("out.txt")

 fprintfn file "Write five = %d" (3+2)
 fprintfn file "Write six = %d" (3+3)
 file.Close()
After evaluation the file contains two lines.

kbprintf continuation builder format

Full Usage: kbprintf continuation builder format

Parameters:
    continuation : unit -> 'Result - The function called after formatting to generate the format result.
    builder : StringBuilder - The input StringBuilder.
    format : BuilderFormat<'T, 'Result> - The input formatter.

Returns: 'T The arguments of the formatter.

bprintf, but call the given 'final' function to generate the result. See kprintf.

continuation : unit -> 'Result

The function called after formatting to generate the format result.

builder : StringBuilder

The input StringBuilder.

format : BuilderFormat<'T, 'Result>

The input formatter.

Returns: 'T

The arguments of the formatter.

Example

Using % format patterns:

 open Printf
 open System.Text

 let buffer = new StringBuilder()

 kbprintf (fun () -> buffer.ToString()) buffer "Write five = %d" (3+2)
Evaluates to "Write five = 5".

kfprintf continuation textWriter format

Full Usage: kfprintf continuation textWriter format

Parameters:
    continuation : unit -> 'Result - The function called after formatting to generate the format result.
    textWriter : TextWriter - The input TextWriter.
    format : TextWriterFormat<'T, 'Result> - The input formatter.

Returns: 'T The arguments of the formatter.

fprintf, but call the given 'final' function to generate the result. See kprintf.

continuation : unit -> 'Result

The function called after formatting to generate the format result.

textWriter : TextWriter

The input TextWriter.

format : TextWriterFormat<'T, 'Result>

The input formatter.

Returns: 'T

The arguments of the formatter.

Example

Using % format patterns:

 open Printf
 open System.IO

 let file = File.CreateText("out.txt")

 kfprintf (fun () -> file.Close()) $"Write three = {1+2}"
Writes "Write five = 5" to out.txt.

kprintf continuation format

Full Usage: kprintf continuation format

Parameters:
    continuation : string -> 'Result - The function called after formatting to generate the format result.
    format : StringFormat<'T, 'Result> - The input formatter.

Returns: 'T The arguments of the formatter.

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.

continuation : string -> 'Result

The function called after formatting to generate the format result.

format : StringFormat<'T, 'Result>

The input formatter.

Returns: 'T

The arguments of the formatter.

Example

Using % format patterns:

 open Printf

 kprintf (fun s -> s + ", done!") $"Write three = {1+2}"
Evaluates to "Write five = 5, done!".

ksprintf continuation format

Full Usage: ksprintf continuation format

Parameters:
    continuation : string -> 'Result - The function called to generate a result from the formatted string.
    format : StringFormat<'T, 'Result> - The input formatter.

Returns: 'T The arguments of the formatter.

sprintf, but call the given 'final' function to generate the result. See kprintf.

continuation : string -> 'Result

The function called to generate a result from the formatted string.

format : StringFormat<'T, 'Result>

The input formatter.

Returns: 'T

The arguments of the formatter.

Example

Using % format patterns:

 open Printf

 ksprintf (fun s -> s + ", done!") $"Write three = {1+2}"
Evaluates to "Write five = 5, done!".

printf format

Full Usage: printf format

Parameters:
Returns: 'T The return type and arguments of the formatter.

Formatted printing to stdout

format : TextWriterFormat<'T>

The input formatter.

Returns: 'T

The return type and arguments of the formatter.

Example

Using interpolated strings:

 printf $"Write three = {1+2}"
After evaluation the text "Write three = 3" is written to stdout.

Example

Using % format patterns:

 printf "Write five = %d" (3+2)
After evaluation the text "Write five = 5" is written to stdout.

printfn format

Full Usage: printfn format

Parameters:
Returns: 'T The return type and arguments of the formatter.

Formatted printing to stdout, adding a newline.

format : TextWriterFormat<'T>

The input formatter.

Returns: 'T

The return type and arguments of the formatter.

Example

Using interpolated strings:

 printfn $"Write three = {1+2}" 
 printfn $"Write four = {2+2}"
After evaluation the two lines are written to stdout.

Example

Using % format patterns:

 printfn "Write five = %d" (3+2)
 printfn "Write six = %d" (3+3)
After evaluation the two lines are written to stdout.

sprintf format

Full Usage: sprintf format

Parameters:
Returns: 'T The formatted string.

Print to a string via an internal string buffer and return the result as a string. Helper printers must return strings.

format : StringFormat<'T>

The input formatter.

Returns: 'T

The formatted string.

Example

 sprintf "Write five = %d and six = %d" (3+2) (3+3)
Evaluates to "Write five = 5 and six = 6".