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))
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.
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
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"
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.