How to avoid% in printfn / sprintf?

How can you print text with the actual% sign in it using printf / sprintf? eg.

let fn = 5 printf "%i%" fn 

gives a compilation error. Obvious \% does not work either.

+6
source share
2 answers

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

In 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 chain the lines, i.e.

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

because

 printf "%%%i" 5 

won't work either.)

+8
source

You can always do something like:

 let fn = 5 printf "%i%s" fn "%" 
+2
source

Source: https://habr.com/ru/post/980656/


All Articles