Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape % in printfn / sprintf?

Tags:

f#

How can you print text with an actual % sign in it using printf/sprintf ? e.g.

let fn = 5
printf "%i%" fn

gives a compile error. The obvious \% doesn't work either.

like image 932
Fsharp Pete Avatar asked Jan 07 '15 13:01

Fsharp Pete


2 Answers

Use "%%" where you want the % in the output text.

From the example above

let fn = 5
printf "%i%%" fn

will happily print "5%"

(Also if you want to print "%5" for some reason, the only way I found is to concatenate strings i.e.

"%"+ (printf "%i" 5)

because

printf "%%%i" 5

will not work either.)

like image 116
Fsharp Pete Avatar answered Sep 19 '22 20:09

Fsharp Pete


You could always do something like:

let fn = 5
printf "%i%s" fn "%"
like image 44
Christopher Stevenson Avatar answered Sep 18 '22 20:09

Christopher Stevenson