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:

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

Post by Jasoco »

I don't need negative numbers. So the one-line solution works. I only use it for money and later XP.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post by Robin »

Jasoco wrote:I don't need negative numbers. So the one-line solution works. I only use it for money and later XP.
So no debts? :P
Help us help you: attach a .love.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post by Robin »

Yeah, you'd need more llamas for that.
Help us help you: attach a .love.
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 »

I removed my postings from this thread in order to not confuse people.
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 »

Your definition of incorrect is incorrect.
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 »

Okay.. I'm bumping my thread to further ask for help.

How can I format a number into a ##,###.## format? Note the decimal and two decimal places at the end. Once again it's for money, but I'm going to be realistic this time and allow cents into the picture instead of whole dollars.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

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

Post by vrld »

As a one-liner:

Code: Select all

string.format("%.2f", number):reverse():gsub("(%d%d%d)(%d)", "%1,%2"):reverse()
For example, if number = 1234.5, it works as following:
  • string.format("%.2f", number) -- exactly two digits after the decimal point: "1234.50"
  • :reverse() -- "05.4321"
  • :gsub("(%d%d%d)(%d)", "%1,%2") -- add a comma after three consecutive decimals.
    the %2 ensures right behavior for numbers like 123, 123456, ... -> result "05.432,1"
  • :reverse() -- "1,234.50"
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
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 »

That fails with big numbers.

When it should be ###,###,###,###.##, it becomes #,####,####,###.##.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

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

Post by vrld »

Damnit. I hoped to avoid another gsub. This works, however:

Code: Select all

string.format("%.2f", number):reverse():gsub("(%d%d%d)", "%1,"):reverse():gsub("^,?", "")
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: Google [Bot], monsieur_h and 30 guests