Strings three ways in F#
printfn formats, sprintf returns, $-interpolation splices.
// Three ways to build strings: printfn formats, sprintf returns,
// and $-interpolation splices inline.
let hive = "Bramble"
let weightKg = 32.6
printfn "%s weighs %.1f kg" hive weightKg
let label = sprintf "%s-%d" hive 2026
printfn "Label: %s" label
let report = $"{hive} is at {weightKg} kg before the flow"
printfn $"Report: {report}"
printfn $"Rounded up: {ceil weightKg}"
How it works
%.1ffixes decimals in the printf family.sprintfbuilds the string instead of printing it.$"{hive} is at {weightKg} kg"interpolates inline.
Keywords and builtins used here
let
The run, in numbers
- Lines
- 13
- Characters to type
- 380
- Tokens
- 51
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 44 seconds.
Step 3 of 3 in Values, step 3 of 27 in Language basics.