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.
- Jasoco
- Inner party member
- Posts: 3725
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Formatting a number into #,###,###?
I don't need negative numbers. So the one-line solution works. I only use it for money and later XP.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Formatting a number into #,###,###?
So no debts?Jasoco wrote:I don't need negative numbers. So the one-line solution works. I only use it for money and later XP.
Help us help you: attach a .love.
- Jasoco
- Inner party member
- Posts: 3725
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Formatting a number into #,###,###?
This isn't Sim City.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Formatting a number into #,###,###?
Yeah, you'd need more llamas for that.
Help us help you: attach a .love.
Re: Formatting a number into #,###,###?
I removed my postings from this thread in order to not confuse people.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Formatting a number into #,###,###?
Your definition of incorrect is incorrect.
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 #,###,###?
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.
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.
Re: Formatting a number into #,###,###?
As a one-liner:
For example, if number = 1234.5, it works as following:
Code: Select all
string.format("%.2f", number):reverse():gsub("(%d%d%d)(%d)", "%1,%2"):reverse()
- 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"
Re: Formatting a number into #,###,###?
That fails with big numbers.
When it should be ###,###,###,###.##, it becomes #,####,####,###.##.
When it should be ###,###,###,###.##, it becomes #,####,####,###.##.
Re: Formatting a number into #,###,###?
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("^,?", "")
Who is online
Users browsing this forum: Google [Bot], monsieur_h and 30 guests