Udostępnij za pośrednictwem


ExtraTopLevelOperators.printfn<'T> Function (F#)

The printfn function prints to stdout using the given format, and adds a newline.

Namespace/Module Path: Microsoft.FSharp.Core.ExtraTopLevelOperators

Assembly: FSharp.Core (in FSharp.Core.dll)

// Signature:
printfn : TextWriterFormat<'T> -> 'T

// Usage:
printfn format

Parameters

Remarks

This function is named PrintFormatLine in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

Example

The following code example demonstrates the use of printfn with various format specifiers. For more information on format specifiers, see Printf Module.

let isDone = false
printfn "Printing Boolean values: %b %b" isDone (not isDone)
let s1,s2 = "test1", @"C:\test2"
printfn "Printing strings (note literal printing of string with special character): %s%s" s1 s2
let i1, i2 = -123, 1891
printfn "Printing an integer in decimal form, with and without a width: %d %10d" i1 i2
printfn "Printing an integer in lowercase hexadecimal: %x or 0x%x" i1 i2
printfn "Printing as an unsigned integer: %u %u" i1 i2
printfn "Printing an integer as uppercase hexadecimal: %X or 0x%X" i1 i2
printfn "Printing as an octal integer: %o %o" i1 i2
printfn "Printing in columns."
for i in 115 .. 59 .. 1000 do
    printfn "%10d%10d%10d%10d%10d" (10100015-i) (i-100) (115+i) (99992/i) (i-8388229)
let x1, x2 = 3.141592654, 6.022E23
printfn "Printing floating point numbers %e %e" x1 x2
printfn "Printing floating point numbers %E %E" x1 x2
printfn "Printing floating point numbers %f %f" x1 x2
printfn "Printing floating point numbers %F %F" x1 x2
printfn "Printing floating point numbers %g %G" x1 x2
printfn "Using the width and precision modifiers: %10.5e %10.3e" x1 x2

printfn "Using the flags:\nZero Pad:|%010d| Plus:|%+10d |LeftJustify:|%-10d| SpacePad:|% d|" 1001 1001 1001 1001 
printfn "zero pad   | |+- both   | |- and ' ' | |' ' and 0 | | normal "
for i in -115 .. 17 .. 100 do
    printfn "|%010d| |%+-10d| |%- 10d| |% 010d| |%10d|" (80-i) (i+85) (100+i) (99992/i) (i-80)

let d = 0.124M
printfn "Decimal: %M" d

printfn "Print as object: %O %O %O %O" 12 1.1 "test" (fun x -> x + 1)

printfn "%A" [| 1; 2; 3 |]

printfn "Printing from a function (no args): %t" (fun writer -> writer.WriteLine("X"))

printfn "Printing from a function with arg: %a" (fun writer (value:int) -> writer.WriteLine("Printing {0}.", value)) 10
Printing Boolean values: false true
Printing strings (note literal printing of string with special character): test1C:\test2
Printing an integer in decimal form, with and without a width: -123       1891
Printing an integer in lowercase hexadecimal: ffffff85 or 0x763
Printing as an unsigned integer: 4294967173 1891
Printing an integer as uppercase hexadecimal: FFFFFF85 or 0x763
Printing as an octal integer: 37777777605 3543
Printing in columns.
  10099900        15       230       869  -8388114
  10099841        74       289       574  -8388055
  10099782       133       348       429  -8387996
  10099723       192       407       342  -8387937
  10099664       251       466       284  -8387878
  10099605       310       525       243  -8387819
  10099546       369       584       213  -8387760
  10099487       428       643       189  -8387701
  10099428       487       702       170  -8387642
  10099369       546       761       154  -8387583
  10099310       605       820       141  -8387524
  10099251       664       879       130  -8387465
  10099192       723       938       121  -8387406
  10099133       782       997       113  -8387347
  10099074       841      1056       106  -8387288
  10099015       900      1115        99  -8387229
Printing floating point numbers 3.141593e+000 6.022000e+023
Printing floating point numbers 3.141593E+000 6.022000E+023
Printing floating point numbers 3.141593 602200000000000000000000.000000
Printing floating point numbers 3.141593 602200000000000000000000.000000
Printing floating point numbers 3.14159 6.022E+23
Using the width and precision modifiers: 3.14159e+000 6.022e+023
Using the flags:
Zero Pad:|0000001001| Plus:|     +1001 |LeftJustify:|1001      | SpacePad:| 1001|
zero pad   | |+- both   | |- and ' ' | |' ' and 0 | | normal 
|0000000195| |-30       | |-15       | |-000000869| |      -195|
|0000000178| |-13       | | 2        | |-000001020| |      -178|
|0000000161| |+4        | | 19       | |-000001234| |      -161|
|0000000144| |+21       | | 36       | |-000001562| |      -144|
|0000000127| |+38       | | 53       | |-000002127| |      -127|
|0000000110| |+55       | | 70       | |-000003333| |      -110|
|0000000093| |+72       | | 87       | |-000007691| |       -93|
|0000000076| |+89       | | 104      | | 000024998| |       -76|
|0000000059| |+106      | | 121      | | 000004761| |       -59|
|0000000042| |+123      | | 138      | | 000002631| |       -42|
|0000000025| |+140      | | 155      | | 000001818| |       -25|
|0000000008| |+157      | | 172      | | 000001388| |        -8|
|-000000009| |+174      | | 189      | | 000001123| |         9|
Decimal: 0.124
Print as object: 12 1.1 test FSI_0006+clo@159-44
[|1; 2; 3|]
Printing from a function (no args): X

Printing from a function with arg: Printing 10.

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Core.ExtraTopLevelOperators Module (F#)

Microsoft.FSharp.Core Namespace (F#)