如何在Lua中不使用科学记数法打印一个巨大的数字?
我正在处理 Lua 中的时间戳,显示自纪元以来的微秒数(例如“1247687475123456”)。
我真的希望能够打印出这个数字的所有可怕的荣耀,但 Lua 坚持用科学记数法打印它。 我已经搜索了有关打印格式化字符串的可用文档,但唯一可用的命令是“以科学计数法打印(%e /%E)”和“如果数字很长则自动以科学计数法打印(%g)” 。 似乎没有选项可以以正常形式打印数字。
我意识到我可以编写一个函数,将原始数字除以 10,然后循环打印数字,但这似乎是一个不优雅的麻烦。 当然有某种内置于该语言中的方法可以做到这一点吗?
I'm dealing with timestamps in Lua showing the number of microseconds since the Epoch (e.g. "1247687475123456").
I would really like to be able to print that number in all its terrible glory, but Lua insists on printing it in scientific notation. I've scoured the available documentation about printing a formatted string, but the only available commands are "Print in scientific notation (%e/%E)" and "Automatically print in scientific notation if the number is very long (%g)". No options seem to be available to print the number in its normal form.
I realize that I could write a function that will take the original number, do some dividing by 10 and print the digits in a loop but that seems like an inelegant hassle. Surely there's some way of doing this that's built in to the language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1247687475123456
1247687475123456
通常配置的 Lua 使用平台常用的双精度浮点格式来存储所有数字。 对于当今的大多数桌面平台来说,这将是 64 位 IEEE-754 格式。 传统观点是可以安全地假定表示 -1E15 到 +1E15 范围内的整数确切地。
无论如何,
string.format()
函数将其参数(经过一些细微的调整)传递给平台的
printf()
。printf()
理解的格式字符串包括%e
和%E
来强制使用“科学”表示法,以及%f
code> 强制使用纯十进制表示法。 此外,%g
和%G
选择最短的表示法。例如:
请注意,如果该值在 32 位有符号整数范围内,您还可以使用
%d
格式。 但是,如果值超出该范围,则结果无法明确定义。 以微秒为单位的系统时间戳可能会超出 32 位范围。如果 16 位十进制数字不够精度,有多种选择可以提高精度。
首先,将真正的 64 位整数与一组合适的算术元方法一起打包在
userdata
中并不困难。 Lua 邮件列表 上偶尔会讨论这个问题,但我不记得看到过完整的任何人发布的模块。其次,Lua 作者之一发布了两个支持任意精度算术的模块: lbc和lmapm。 两者都可以在该页面找到。
第三,在 Google 中随意搜索很容易找到其他几个数学库包装器。
Lua as usually configured uses your platform's usual double-precision floating point format to store all numbers. For most desktop platforms today, that will be the 64-bit IEEE-754 format. The conventional wisdom is that integers in the range -1E15 to +1E15 can be safely assumed to be represented exactly.
In any case, the
string.format()
function passes its arguments through (with some minor tweaks) to the platform's implementation ofprintf()
. The format string understood byprintf()
includes%e
and%E
to force "scientific" notation, and%f
to force plain decimal notation. In addition,%g
and%G
choose the shortest notation.For example:
Note that if the value fits within a 32-bit signed integer range, you can also use the
%d
format. However, results are not well defined if the value exceeds that range. System timestamps in microseconds are likely to exceed the 32-bit range.If 16 decimal digits is not enough precision, there are several choices available for increased precision.
First, it would not be difficult to package a true 64-bit integer in a
userdata
along with a suitable set of arithmetic metamethods. This gets discussed occasionally on the Lua mailing list, but I don't recall seeing a completed module released by anyone.Second, one of the Lua authors has released two modules supporting arbitrary precision arithmetic: lbc and lmapm. Both are found at that page.
Third, casual searching in Google readily turns up several other math library wrappers.