在Lua中,可以使用字符串的string.format函数来格式化输出。这个函数的语法如下:

string.format(formatstring, ...)

其中,formatstring是一个格式化字符串,用于指定输出的格式,...是要输出的值。

以下是一些常用的格式化字符串的示例:

  • %s:输出字符串
  • %d:输出整数
  • %f:输出浮点数
  • %c:输出一个字符
  • %o:输出八进制数
  • %x:输出十六进制数
  • %e:输出科学计数法表示的浮点数
  • %%:输出百分号

下面是一个例子,演示如何使用string.format函数来格式化输出:

local name = "Alice"
local age = 25
local height = 1.65

print(string.format("My name is %s, I'm %d years old and %.2f meters tall.", name, age, height))

运行以上代码,会输出:

My name is Alice, I'm 25 years old and 1.65 meters tall.

在格式化字符串中,%s表示要输出一个字符串,%d表示要输出一个整数,%.2f表示要输出一个保留两位小数的浮点数

lua 格式化输出

原文地址: https://www.cveoy.top/t/topic/iSaW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录