Formatting a number into #,###,###?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Formatting a number into #,###,###?

Post by Jasoco »

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?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Formatting a number into #,###,###?

Post by kikito »

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:

Code: Select all

local number = 1234567890
local snumber = string.format("%d", number) -- "1234567890"
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:

Code: Select all

snumber:reverse():gsub( "(%d%d%d)" , "%1," ):reverse():gsub("^,","") -- "1,234,567,890"
If you want the complete thing:

Code: Select all

function commaformat(number)
   return string.format("%d", number):reverse():gsub( "(%d%d%d)" , "%1," ):reverse():gsub("^,","")
end
Simple, huh? :)
Last edited by kikito on Thu Aug 12, 2010 10:09 am, edited 1 time in total.
When I write def I mean function.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Formatting a number into #,###,###?

Post by Jasoco »

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.
pekka
Party member
Posts: 206
Joined: Thu Jan 07, 2010 6:48 am
Location: Oulu, Finland
Contact:

Re: Formatting a number into #,###,###?

Post by pekka »

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.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Formatting a number into #,###,###?

Post by thelinx »

Here's my (one-line) solution:

Code: Select all

local formattedNumber = (tostring(myNumber):reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", ""))
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Formatting a number into #,###,###?

Post by kikito »

Oh, yes, tostring. I forgot about it.
When I write def I mean function.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Formatting a number into #,###,###?

Post by thelinx »

Hey, I didn't see you edited your post? Blasphemy! I was the original master of everything pure and shiny!
:cry:
pekka
Party member
Posts: 206
Joined: Thu Jan 07, 2010 6:48 am
Location: Oulu, Finland
Contact:

Re: Formatting a number into #,###,###?

Post by pekka »

EDIT: Posting removed
Last edited by pekka on Sat Aug 14, 2010 8:44 am, edited 1 time in total.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Formatting a number into #,###,###?

Post by Jasoco »

Thanks! They work great! I knew there was a fairly simple one-line solution.
pekka
Party member
Posts: 206
Joined: Thu Jan 07, 2010 6:48 am
Location: Oulu, Finland
Contact:

Re: Formatting a number into #,###,###?

Post by pekka »

EDIT: Posting removed
Last edited by pekka on Sat Aug 14, 2010 8:44 am, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 62 guests