I know it's simple, but I don't know the syntax.
How would I format a number like 93522341 into 93,522,341? Adding commas?
Formatting a number into #,###,###?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Formatting a number into #,###,###?
I can't help you much, but I can give you a start.
Lua provides a string.format function, which will convert a number to string, like so:
Now the problem has been transformed in "insert one comma between every 3 characters of a string". This is probably doable with regular expressions. But I can't help you with that part. Lua's reliance on regular expressions for string manipulation is one of the things I don't like about it.
Good luck!
EDIT: OK I got this:
If you want the complete thing:
Simple, huh?
Lua provides a string.format function, which will convert a number to string, like so:
Code: Select all
local number = 1234567890
local snumber = string.format("%d", number) -- "1234567890"
Good luck!
EDIT: OK I got this:
Code: Select all
snumber:reverse():gsub( "(%d%d%d)" , "%1," ):reverse():gsub("^,","") -- "1,234,567,890"
Code: Select all
function commaformat(number)
return string.format("%d", number):reverse():gsub( "(%d%d%d)" , "%1," ):reverse():gsub("^,","")
end
Last edited by kikito on Thu Aug 12, 2010 10:09 am, edited 1 time in total.
When I write def I mean function.
- Jasoco
- Inner party member
- Posts: 3725
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Formatting a number into #,###,###?
Sad. In QBASIC there was a simple PRINT "#,###,###", var that could be called that would format the number for you. But I've never seen it anywhere else. So I've always had to just dissect the number as a string into three character segments and glue them back together. I was hoping Lua had a method to work around that. Guess not. Oh well. I'll have to do it the same way I always do.
Re: Formatting a number into #,###,###?
EDIT: Posting removed because this thread is only for incorrect solutions
Last edited by pekka on Sat Aug 14, 2010 8:44 am, edited 1 time in total.
Re: Formatting a number into #,###,###?
Here's my (one-line) solution:
Code: Select all
local formattedNumber = (tostring(myNumber):reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", ""))
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Formatting a number into #,###,###?
Oh, yes, tostring. I forgot about it.
When I write def I mean function.
Re: Formatting a number into #,###,###?
Hey, I didn't see you edited your post? Blasphemy! I was the original master of everything pure and shiny!
Re: Formatting a number into #,###,###?
EDIT: Posting removed
Last edited by pekka on Sat Aug 14, 2010 8:44 am, edited 1 time in total.
- Jasoco
- Inner party member
- Posts: 3725
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Formatting a number into #,###,###?
Thanks! They work great! I knew there was a fairly simple one-line solution.
Re: Formatting a number into #,###,###?
EDIT: Posting removed
Last edited by pekka on Sat Aug 14, 2010 8:44 am, edited 1 time in total.
Who is online
Users browsing this forum: No registered users and 62 guests