lisu
lisu

Reputation: 2263

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?

Upvotes: 3

Views: 3584

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 81012

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.

Upvotes: 5

Related Questions