如何在不使用科学计数法的情况下在 Lua 中打印一个巨大的数字? [英] How can I print a huge number in Lua without using scientific notation?

查看:47
本文介绍了如何在不使用科学计数法的情况下在 Lua 中打印一个巨大的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 Lua 中的时间戳,显示自 Epoch 以来的微秒数(例如1247687475123456").

I'm dealing with timestamps in Lua showing the number of microseconds since the Epoch (e.g. "1247687475123456").

真的希望能够打印出这个数字的所有可怕的荣耀,但 Lua 坚持用科学记数法打印它.我已经搜索了有关打印格式化字符串的可用文档,但唯一可用的命令是以科学记数法打印 (%e/%E)"和如果数字很长,则自动以科学记数法打印 (%g)".似乎没有选项可以以正常形式打印数字.

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.

我意识到我可以编写一个函数来获取原始数字,除以 10 并在循环中打印数字,但这似乎是一个不雅的麻烦.肯定有一些内置于语言的方法吗?

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?

推荐答案

> print(string.format("%18.0f",1247687475123456))

1247687475123456

1247687475123456

这篇关于如何在不使用科学计数法的情况下在 Lua 中打印一个巨大的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆