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 |
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. |
|
Represents a statically-analyzed format associated with writing to a StringBuilder. The type parameter indicates the arguments and return type of the format operation. |
|
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. |
|
Represents a statically-analyzed format when formatting builds a string. The type parameter indicates the arguments and return type of the format operation. |
|
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. |
|
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
|
Full Usage:
bprintf builder format
Parameters:
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.
|
Print to a StringBuilder
ExampleUsing interpolated strings:
module Printf
from Microsoft.FSharp.Core
namespace System
namespace System.Text
val buffer: StringBuilder
Multiple items
type StringBuilder = interface ISerializable new: unit -> unit + 5 overloads member Append: value: bool -> StringBuilder + 25 overloads member AppendFormat: provider: IFormatProvider * format: string * arg0: obj -> StringBuilder + 14 overloads member AppendJoin: separator: char * [<ParamArray>] values: obj array -> StringBuilder + 9 overloads member AppendLine: unit -> StringBuilder + 3 overloads member Clear: unit -> StringBuilder member CopyTo: sourceIndex: int * destination: char array * destinationIndex: int * count: int -> unit + 1 overload member EnsureCapacity: capacity: int -> int member Equals: span: ReadOnlySpan<char> -> bool + 1 overload ... <summary>Represents a mutable string of characters. This class cannot be inherited.</summary> -------------------- StringBuilder() : StringBuilder StringBuilder(capacity: int) : StringBuilder StringBuilder(value: string) : StringBuilder StringBuilder(capacity: int, maxCapacity: int) : StringBuilder StringBuilder(value: string, capacity: int) : StringBuilder StringBuilder(value: string, startIndex: int, length: int, capacity: int) : StringBuilder val bprintf: builder: StringBuilder -> format: BuilderFormat<'T> -> 'T
StringBuilder.ToString() : string
Evaluates to StringBuilder.ToString(startIndex: int, length: int) : string "Write three = 3" .
Example
Using
module Printf
from Microsoft.FSharp.Core
namespace System
namespace System.Text
val buffer: StringBuilder
Multiple items
type StringBuilder = interface ISerializable new: unit -> unit + 5 overloads member Append: value: bool -> StringBuilder + 25 overloads member AppendFormat: provider: IFormatProvider * format: string * arg0: obj -> StringBuilder + 14 overloads member AppendJoin: separator: char * [<ParamArray>] values: obj array -> StringBuilder + 9 overloads member AppendLine: unit -> StringBuilder + 3 overloads member Clear: unit -> StringBuilder member CopyTo: sourceIndex: int * destination: char array * destinationIndex: int * count: int -> unit + 1 overload member EnsureCapacity: capacity: int -> int member Equals: span: ReadOnlySpan<char> -> bool + 1 overload ... <summary>Represents a mutable string of characters. This class cannot be inherited.</summary> -------------------- StringBuilder() : StringBuilder StringBuilder(capacity: int) : StringBuilder StringBuilder(value: string) : StringBuilder StringBuilder(capacity: int, maxCapacity: int) : StringBuilder StringBuilder(value: string, capacity: int) : StringBuilder StringBuilder(value: string, startIndex: int, length: int, capacity: int) : StringBuilder val bprintf: builder: StringBuilder -> format: BuilderFormat<'T> -> 'T
StringBuilder.ToString() : string
Evaluates to StringBuilder.ToString(startIndex: int, length: int) : string "Write five = 5" .
|
Full Usage:
eprintf format
Parameters:
TextWriterFormat<'T>
-
The input formatter.
Returns: 'T
The return type and arguments of the formatter.
|
Formatted printing to stderr
ExampleUsing interpolated strings:
val eprintf: format: Printf.TextWriterFormat<'T> -> 'T
After evaluation the text "Write three = 3" is written to stderr .
Example
Using
val eprintf: format: Printf.TextWriterFormat<'T> -> 'T
After evaluation the text "Write five = 5" is written to stderr .
|
Full Usage:
eprintfn format
Parameters:
TextWriterFormat<'T>
-
The input formatter.
Returns: 'T
The return type and arguments of the formatter.
|
Formatted printing to stderr, adding a newline
ExampleUsing interpolated strings:
val eprintfn: format: Printf.TextWriterFormat<'T> -> 'T
After evaluation two lines are written to stderr .
Example
Using
val eprintfn: format: Printf.TextWriterFormat<'T> -> 'T
After evaluation two lines are written to stderr .
|
Full Usage:
failwithf format
Parameters:
StringFormat<'T, 'Result>
-
The input formatter.
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.
Example
val failwithf: format: Printf.StringFormat<'T,'Result> -> 'T
Throws Exception with message "That's wrong. Five = 5 and six = 6" .
|
Full Usage:
fprintf textWriter format
Parameters:
TextWriter
-
The TextWriter to print to.
format : TextWriterFormat<'T>
-
The input formatter.
Returns: 'T
The return type and arguments of the formatter.
|
Print to a text writer.
ExampleUsing interpolated strings:
module Printf
from Microsoft.FSharp.Core
namespace System
namespace System.IO
val file: StreamWriter
type File =
static member AppendAllBytes: path: string * bytes: byte array -> unit + 1 overload
static member AppendAllBytesAsync: path: string * bytes: byte array * ?cancellationToken: CancellationToken -> Task + 1 overload
static member AppendAllLines: path: string * contents: IEnumerable<string> -> unit + 1 overload
static member AppendAllLinesAsync: path: string * contents: IEnumerable<string> * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 1 overload
static member AppendAllText: path: string * contents: string -> unit + 3 overloads
static member AppendAllTextAsync: path: string * contents: string * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 3 overloads
static member AppendText: path: string -> StreamWriter
static member Copy: sourceFileName: string * destFileName: string -> unit + 1 overload
static member Create: path: string -> FileStream + 2 overloads
static member CreateSymbolicLink: path: string * pathToTarget: string -> FileSystemInfo
...
<summary>Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of <see cref="T:System.IO.FileStream" /> objects.</summary> File.CreateText(path: string) : StreamWriter
val fprintf: textWriter: TextWriter -> format: TextWriterFormat<'T> -> 'T
StreamWriter.Close() : unit
After evaluation the file contains the text "Write three = 3" .
Example
Using
module Printf
from Microsoft.FSharp.Core
namespace System
namespace System.IO
val file: StreamWriter
type File =
static member AppendAllBytes: path: string * bytes: byte array -> unit + 1 overload
static member AppendAllBytesAsync: path: string * bytes: byte array * ?cancellationToken: CancellationToken -> Task + 1 overload
static member AppendAllLines: path: string * contents: IEnumerable<string> -> unit + 1 overload
static member AppendAllLinesAsync: path: string * contents: IEnumerable<string> * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 1 overload
static member AppendAllText: path: string * contents: string -> unit + 3 overloads
static member AppendAllTextAsync: path: string * contents: string * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 3 overloads
static member AppendText: path: string -> StreamWriter
static member Copy: sourceFileName: string * destFileName: string -> unit + 1 overload
static member Create: path: string -> FileStream + 2 overloads
static member CreateSymbolicLink: path: string * pathToTarget: string -> FileSystemInfo
...
<summary>Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of <see cref="T:System.IO.FileStream" /> objects.</summary> File.CreateText(path: string) : StreamWriter
val fprintf: textWriter: TextWriter -> format: TextWriterFormat<'T> -> 'T
StreamWriter.Close() : unit
After evaluation the file contains the text "Write five = 5" .
|
Full Usage:
fprintfn textWriter format
Parameters:
TextWriter
-
The TextWriter to print to.
format : TextWriterFormat<'T>
-
The input formatter.
Returns: 'T
The return type and arguments of the formatter.
|
Print to a text writer, adding a newline
ExampleUsing interpolated strings:
module Printf
from Microsoft.FSharp.Core
namespace System
namespace System.IO
val file: StreamWriter
type File =
static member AppendAllBytes: path: string * bytes: byte array -> unit + 1 overload
static member AppendAllBytesAsync: path: string * bytes: byte array * ?cancellationToken: CancellationToken -> Task + 1 overload
static member AppendAllLines: path: string * contents: IEnumerable<string> -> unit + 1 overload
static member AppendAllLinesAsync: path: string * contents: IEnumerable<string> * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 1 overload
static member AppendAllText: path: string * contents: string -> unit + 3 overloads
static member AppendAllTextAsync: path: string * contents: string * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 3 overloads
static member AppendText: path: string -> StreamWriter
static member Copy: sourceFileName: string * destFileName: string -> unit + 1 overload
static member Create: path: string -> FileStream + 2 overloads
static member CreateSymbolicLink: path: string * pathToTarget: string -> FileSystemInfo
...
<summary>Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of <see cref="T:System.IO.FileStream" /> objects.</summary> File.CreateText(path: string) : StreamWriter
val fprintfn: textWriter: TextWriter -> format: TextWriterFormat<'T> -> 'T
StreamWriter.Close() : unit
After evaluation the file contains two lines.
Example
Using
module Printf
from Microsoft.FSharp.Core
namespace System
namespace System.IO
val file: StreamWriter
type File =
static member AppendAllBytes: path: string * bytes: byte array -> unit + 1 overload
static member AppendAllBytesAsync: path: string * bytes: byte array * ?cancellationToken: CancellationToken -> Task + 1 overload
static member AppendAllLines: path: string * contents: IEnumerable<string> -> unit + 1 overload
static member AppendAllLinesAsync: path: string * contents: IEnumerable<string> * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 1 overload
static member AppendAllText: path: string * contents: string -> unit + 3 overloads
static member AppendAllTextAsync: path: string * contents: string * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 3 overloads
static member AppendText: path: string -> StreamWriter
static member Copy: sourceFileName: string * destFileName: string -> unit + 1 overload
static member Create: path: string -> FileStream + 2 overloads
static member CreateSymbolicLink: path: string * pathToTarget: string -> FileSystemInfo
...
<summary>Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of <see cref="T:System.IO.FileStream" /> objects.</summary> File.CreateText(path: string) : StreamWriter
val fprintfn: textWriter: TextWriter -> format: TextWriterFormat<'T> -> 'T
StreamWriter.Close() : unit
After evaluation the file contains two lines.
|
Full Usage:
kbprintf continuation builder format
Parameters:
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
Example
Using
module Printf
from Microsoft.FSharp.Core
namespace System
namespace System.Text
val buffer: StringBuilder
Multiple items
type StringBuilder = interface ISerializable new: unit -> unit + 5 overloads member Append: value: bool -> StringBuilder + 25 overloads member AppendFormat: provider: IFormatProvider * format: string * arg0: obj -> StringBuilder + 14 overloads member AppendJoin: separator: char * [<ParamArray>] values: obj array -> StringBuilder + 9 overloads member AppendLine: unit -> StringBuilder + 3 overloads member Clear: unit -> StringBuilder member CopyTo: sourceIndex: int * destination: char array * destinationIndex: int * count: int -> unit + 1 overload member EnsureCapacity: capacity: int -> int member Equals: span: ReadOnlySpan<char> -> bool + 1 overload ... <summary>Represents a mutable string of characters. This class cannot be inherited.</summary> -------------------- StringBuilder() : StringBuilder StringBuilder(capacity: int) : StringBuilder StringBuilder(value: string) : StringBuilder StringBuilder(capacity: int, maxCapacity: int) : StringBuilder StringBuilder(value: string, capacity: int) : StringBuilder StringBuilder(value: string, startIndex: int, length: int, capacity: int) : StringBuilder val kbprintf: continuation: (unit -> 'Result) -> builder: StringBuilder -> format: BuilderFormat<'T,'Result> -> 'T
StringBuilder.ToString() : string
Evaluates to StringBuilder.ToString(startIndex: int, length: int) : string "Write five = 5" .
|
Full Usage:
kfprintf continuation textWriter format
Parameters:
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
Example
Using
module Printf
from Microsoft.FSharp.Core
namespace System
namespace System.IO
val file: StreamWriter
type File =
static member AppendAllBytes: path: string * bytes: byte array -> unit + 1 overload
static member AppendAllBytesAsync: path: string * bytes: byte array * ?cancellationToken: CancellationToken -> Task + 1 overload
static member AppendAllLines: path: string * contents: IEnumerable<string> -> unit + 1 overload
static member AppendAllLinesAsync: path: string * contents: IEnumerable<string> * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 1 overload
static member AppendAllText: path: string * contents: string -> unit + 3 overloads
static member AppendAllTextAsync: path: string * contents: string * encoding: Encoding * ?cancellationToken: CancellationToken -> Task + 3 overloads
static member AppendText: path: string -> StreamWriter
static member Copy: sourceFileName: string * destFileName: string -> unit + 1 overload
static member Create: path: string -> FileStream + 2 overloads
static member CreateSymbolicLink: path: string * pathToTarget: string -> FileSystemInfo
...
<summary>Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of <see cref="T:System.IO.FileStream" /> objects.</summary> File.CreateText(path: string) : StreamWriter
val kfprintf: continuation: (unit -> 'Result) -> textWriter: TextWriter -> format: TextWriterFormat<'T,'Result> -> 'T
StreamWriter.Close() : unit
Writes "Write three = 3" to out.txt .
|
Full Usage:
kprintf continuation format
Parameters:
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.
Example
Using
module Printf
from Microsoft.FSharp.Core
val kprintf: continuation: (string -> 'Result) -> format: StringFormat<'T,'Result> -> 'T
val s: string
Evaluates to "Write three = 3, done!" .
|
Full Usage:
ksprintf continuation format
Parameters:
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
Example
Using
module Printf
from Microsoft.FSharp.Core
val ksprintf: continuation: (string -> 'Result) -> format: StringFormat<'T,'Result> -> 'T
val s: string
Evaluates to "Write three = 3, done!" .
|
Full Usage:
printf format
Parameters:
TextWriterFormat<'T>
-
The input formatter.
Returns: 'T
The return type and arguments of the formatter.
|
Formatted printing to stdout
ExampleUsing interpolated strings:
val printf: format: Printf.TextWriterFormat<'T> -> 'T
After evaluation the text "Write three = 3" is written to stdout .
Example
Using
val printf: format: Printf.TextWriterFormat<'T> -> 'T
After evaluation the text "Write five = 5" is written to stdout .
|
Full Usage:
printfn format
Parameters:
TextWriterFormat<'T>
-
The input formatter.
Returns: 'T
The return type and arguments of the formatter.
|
Formatted printing to stdout, adding a newline.
ExampleUsing interpolated strings:
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
After evaluation the two lines are written to stdout .
Example
Using
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
After evaluation the two lines are written to stdout .
|
Full Usage:
sprintf format
Parameters:
StringFormat<'T>
-
The input formatter.
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.
Example
val sprintf: format: Printf.StringFormat<'T> -> 'T
Evaluates to "Write five = 5 and six = 6" .
|