In this post we’ll discuss the very basic function that everyone uses in any language. When someone starts learning programming language the very first thing they want to do is printing/writing out on console.
The Print
F# has two basic functions; “printf” and “printfn” to print out the information on console.
- // Theprintf/printfn functions are similar to the
- // Console.Write/WriteLine functions in C#.
- printfn "Hello %s" "World"
- printfn "it's %d" 2016
The print functions in F# are statically type checked equivalent to C-style printing. So any argument will be type checked with format specifiers. Below is list of F# format specifiers.
- %s for strings
- %b for bools
- %i or %d for signed ints%u for unsigned ints
- %f for floats
- %A for pretty-printing tuples, records and union types, BigInteger and all complex types
- %O for other objects, using ToString()
- %x and %X for lowercase and uppercase hex
- %o for octal
- %f for floats standard format
- %e or %E for exponential format
- %g or %G for the more compact of f and e.
- %M for decimals
Padded formatting
- %0i pads with zeros
- %+i shows a plus sign
- % i shows a blank in place of a plus sign
Date Formatting
There’s not built-in format specifier for date type. There are two options to format dates:
- With override of ToString() method
- Using a custom callback with ‘%a’ specifier which allows a callback function that take TextWriter as argument.
With option 1:
- // function to format a date
- let yymmdd1 (date:System.DateTime) = date.ToString("MM/dd/YYYY")
- [<EntryPoint>]
- let main argv =
- printfn "using ToString = %s" (yymmdd1 System.DateTime.Now)
- System.Console.ReadKey() |> ignore// To hold the console window
- 0 // return an integer exit code
- // function to format a date onto a TextWriter
- let yymmdd2 (tw:System.IO.TextWriter) (date:DateTime) = tw.Write("{0:yy.MM.dd}", date)
Of course here option 1 is much easier but you can go with any of the options.
Note: Printing ‘%’ character will require escaping as it has special meaning.
- printfn "unescaped: %" // error
- printfn "escape: %%"
Read out more about F# print formatters here.
Read more articles on F#:

Rahul Kumar SaxenaPosted Apr 2, 2016, 12:21 PM
Good Show...
Gowtham RajamanickamPosted Apr 2, 2016, 3:37 AM
good one...
Shaili DashoraPosted Apr 2, 2016, 3:28 AM
Good Job,Nice one...
Mahesh ChandPosted Apr 1, 2016, 11:50 PM
Great. Looks like you're doing F# a lot. \
Saillesh PawarPosted Apr 1, 2016, 2:56 PM
Amit sir my main concern in what areas fsharp is better than c#. Is it specific for complex computing or something more specific.
Vignesh ManiPosted Apr 1, 2016, 10:18 AM
good
Mohammed IbrahimPosted Apr 1, 2016, 6:09 AM
nice