Format number to separate thousand values (eg 12000000 would become 12 000 000)

不打扰是莪最后的温柔 提交于 2019-12-10 22:16:53

问题


In lua, I would like to format an integer (or float) number to separate every three decimal places with a space (or a coma, as in US), so for example number 120000010 would be presented as 120 000 010 or 120,000,010

I have found this, but is there a way to achieve this using string.format, or any other way not involving custom implementation?


回答1:


printf (and friends) can do it with the (according to the man page) SUSv2 format flag '.

So printf "%'d\n" 1000000 outputs 1,000,000 but lua doesn't seem to support passing that through to the system printf calls.

To clarify this answer for whoever down-voted me. The answer here is "No". The only way to use string.format for this would be to use that flag which, as indicated, lua does not pass through. As such there is no way, that I'm aware of, to do this without a custom solution such as that linked in the OP's question.



来源:https://stackoverflow.com/questions/25726003/format-number-to-separate-thousand-values-eg-12000000-would-become-12-000-000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!