Better guide to roblox strings and formatting?

Hey, I have been messing around with strings and I am really confused on string formatting, ect.
I was wondering if anyone has some videos or better guides to how strings work and how to format them. The wiki helps a little bit, but there is still a lot left unclear for me. I guess I just do not understand it fully.

Here is a example:

local percentage = 123.4345

print(("%.2f%%"):format(percentage))
  1. I got this from a post about rounding to a decimal place, but I cant get it to work other ways. The output comes out as 123.43%, so to get rid of the % at the end I just remove one of the %'s I would think. But then it gives me a error “Missing arguments”
    I just dont even know how it works.

  2. The wiki says

For e / E and f specifiers, this is the number of digits to be printed after the decimal point.

Does that mean that f, e ,E do the same thing, or what?

If anyone has a bit of advice or any tutorials, links, ect. that would be greatly appreciated.
Thanks :mask:

2 Likes

% followed by another % will return the % sign itself.

Remove both %'s. In format strings, % means “the character following this is special”. You put %% if you want it to just be a normal %.

You remove both percents so the pattern is just %.2f. Since a % sign has a special meaning in string patterns and format specifiers it needs to be escaped by putting another % sign in front.

f means float, and the number 2 is saying it will round to 2 decimal places and do proper rounding, so ("%.2f"):format(123.466) == "123.47"

@sjr04, and @nicemike40 Thanks for the help! Do you know of any better places to learn strings and stuff thoroughly?

I don’t think you can get much better than the ROBLOX wiki for correct, accessible information, honestly.

What’s confusing you specifically?

A format string is just a string with some possibly special parts, marked by starting with a %, that look like this:

% [flags] [width] [.precision] specifier

(the [stuff] is optional). That page then has sections dedicated to each of those: flags, width, precision, and specifier, and lays out what the options are for each of them, examples, etc.

1 Like