Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 293
lisu Avatar asked Oct 20 '25 03:10

lisu


1 Answers

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.

like image 154
Etan Reisner Avatar answered Oct 22 '25 05:10

Etan Reisner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!