Lua string.format() use collation

Posted by tiagofrancis on Sun, 12 Sep 2021 07:26:53 +0200

In the actual development process using Lua, the frequency of using the function string.format() to format a string is very high. Here is a summary of the use of this function.

Parameter analysis

string.format(formatstring, ...)

formatstring   Is a format string (must be a string), which may contain multiple escape codes (such as the most commonly used% d% s, etc.).

...   It is a variable number of parameter lists, separated by commas, and corresponds to the escape codes in the format string one by one.

e.g.

print(string.format("%s Have %d Olympic Gold Medals!", "Kevin Durant", 3))
-- Kevin Durant Have 3 Olympic Gold Medals!

Escape code

The escape codes supported in the format string are as follows:

%c

Receive a number and convert it to the corresponding character in the ASCII code table.

print(string.format("%c", 65))  -- A
print(string.format("%c", 97))  -- a

%d

Receives a number and converts it to signed integer format. (% i is consistent with% d)

print(string.format("%d", 666))  -- 666
print(string.format("%d", -666))  -- -666

%e

Receive a number and convert it to Scientific notation Format, using the lowercase letter e.

print(string.format("%e", 666))  -- 6.660000e+002
print(string.format("%e", -666))  -- -6.660000e+002

%E

Receive a number and convert it to scientific notation format using the capital letter E.

print(string.format("%E", 666))  -- 6.660000E+002
print(string.format("%E", -666))  -- -6.660000E+002

%f

Receives a number and converts it to floating point format.

print(string.format("%f", 666))  -- 666.000000
print(string.format("%f", -666))  -- -666.000000

%g

Receive a number and convert it to the shorter of% e and% f.

print(string.format("%g", 666666))  -- 666666
print(string.format("%g", -6666666))  -- -6.66667e+006

%G

Receive a number and convert it to the shorter of% G and% f.

print(string.format("%G", 666666))  -- 666666
print(string.format("%G", -6666666))  -- -6.66667E+006

%o

Receive a number and convert it to octal number format.

print(string.format("%o", 10))  -- 12
print(string.format("%o", 17))  -- 21

%q

Receives a string and converts it to a format that can be safely read by the Lua compiler.

print(string.format('%q', 'Hello \n "fightsyj" !'))
-- "Hello \
--  \"fightsyj\" !"

%s

Receives a string and formats it according to the given parameters.

print(string.format("Hello %s !", "fightsyj"))  -- Hello fightsyj !

%u

Receives a number and converts it to unsigned integer format.

print(string.format("%u", 666))  -- 666
print(string.format("%u", -1))  -- 4294967295

In the above example, when formatting - 1 with% u, a large number is output Explain in detail.

%x

Receive a number and convert it to hexadecimal number format, using lowercase letters.

print(string.format("%x", 15))  -- f
print(string.format("%x", 666))  -- 29a

%X

Receive a number and convert it to hexadecimal number format, using uppercase letters.

print(string.format("%X", 15))  -- F
print(string.format("%X", 666))  -- 29A

Escape code parameters

To further refine the format, you can also add parameters after% of the escape code:

+

Indicates that the subsequent numeric escape code will cause a positive number to display a positive sign. (by default, only negative numbers display symbols)

print(string.format("%d", -666))  -- -666
print(string.format("%d", 666))  -- 666
print(string.format("%+d", -666))  -- -666
print(string.format("%+d", 666))  -- +666

0

It is used when the string width is specified later (the default placeholder when not filled in is a space).

print(string.format("%d", 666))  -- 666
print(string.format("%5d", 666))   --   666
print(string.format("%05d", 666))  -- 00666

-

When the string width is specified, the default is right alignment, and the increase - can be changed to left alignment.

print(string.format("%d", 666))  -- 666
print(string.format("%5d", 666))  --   666
print(string.format("%-5d", 666))  -- 666  

.

When the escape code is% f, it is followed by the number n, indicating the reserved N decimal places;
When the escape code is% s, it is followed by the number n, indicating that the first n digits are displayed.

-- %f
print(string.format("%f", 666))  -- 666.000000
print(string.format("%0.2f", 666))  -- 666.00
print(string.format("%.2f", 666))  -- 666.00

-- %s
print(string.format("%s", "Hello"))  -- Hello
print(string.format("%0.2s", "Hello"))  -- He
print(string.format("%.2s", "Hello"))  -- He

ps

  • The order of adding parameters to the escape code is:% [specify parameter] [identifier] [width] [. Precision] indicator.
  • Use string.format()   Output% requires%%.

e.g.

print(string.format("%d%%", 50))  -- 50%

Topics: lua