简体   繁体   中英

How to escape % in printfn / sprintf?

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

let fn = 5
printf "%i%" fn

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

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 ie

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

because

printf "%%%i" 5

will not work either.)

You could always do something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM