What is the purpose of %c specifier in stringformat?

Recently I started dabbing into large number abbreviations turning pure numbers like 1000 to 1,000 or 1k and I came across string.format which could allow me to make that happen. I read through it all and I know how different specifiers work.

For example:

local foo = 1234.5

print(string.format("The Magic Number is %d",foo)) -->  The Magic Number is 1234
------------------------------------------------------------------------------------
print(string.format("To allow decimal, %.1f",foo)) --> To allow decimal, 1234.5
------------------------------------------------------------------------------------
print(string.format("The exponent of %d is %e", foo, foo) --> The exponent of 12345 is 1.2345e+03
------------------------------------------------------------------------------------
-- And then you get to something like this
string.format("This is the result, %c",foo)) --> This is the result,  ďż˝

It can format integers like %d or %i specifier but out of the entire specifiers this one just prints symbols. That begs the question, what does %c do?

The information on this.

It looks like it is used to turn numbers into icons? String Patterns (roblox.com) I think it is for different characters, like emojis that you can not usually print.

Ok, just looked a little harder and found this aritcle which ROBLOX linked. Apparently it is for “control characters.” Control character - Wikipedia

I read the article a little bit. Pretty advanced stuff going around that doesn’t get seen by the player. Anyway this gets my curiosity on it over so thank you for the help.

1 Like

No problem! :smiley: Glad I could help!

The %c (char) specifier is for converting numbers into characters:

local foo = 65

print(string.format("I'm the letter %c",foo)) -- I'm the letter A

Not sure why its not documented on the dev hub, but assuming roblox uses the standard lua string libraries’ format function, then all the specifiers (expect %q) follow C’s printf specifiers; From the Lua 5.1 manual:

The format string follows the same rules as the printf family of standard C functions…[expect for the %q specifier and modifiers]

5 Likes

Then what about the symbols and control characters?

You got the question mark symbol when you used 1234.5 because it isn’t a valid ASCII decimal value. (they only go up to 127).


As for control characters, they aren’t really related to format strings themselves; they are used in a string to denote a specific action, like \n or \r. In the case of pattern matching %c means something different than with string formatting, it’s a character class meant to match with control characters:

local String = "This is a line \n This is another line"

print(String) --[[
This is a line 
This is another line
]]
print(string.match(String, "%c(.+)")) --"This is another line"
3 Likes