Smirky
|
  |
| Joined: 05 Apr 2008 |
| Total Posts: 3598 |
|
|
| 29 Dec 2011 05:51 PM |
Hi guys,
I don't need this for any particular reason, but I want to know if there is a print function to add commas to the output when printing a number like 1000000 (as a number, not string)?
For example, printing 1000000 as 1,000,000?
Thanks. |
|
|
| Report Abuse |
|
|
Xsitsu
|
  |
| Joined: 28 Jul 2009 |
| Total Posts: 2921 |
|
|
| 29 Dec 2011 05:59 PM |
| I believe you would have to make your own. |
|
|
| Report Abuse |
|
|
|
| 29 Dec 2011 06:16 PM |
That will require a custom function. :)
I have it but it's not letting me post it.
-metaplates, Master of JavaScript. |
|
|
| Report Abuse |
|
|
|
| 29 Dec 2011 06:26 PM |
| It's not letting me post mine. I'll PM it to you. |
|
|
| Report Abuse |
|
|
Smirky
|
  |
| Joined: 05 Apr 2008 |
| Total Posts: 3598 |
|
|
| 29 Dec 2011 06:31 PM |
@Merlin,
If you were talking to me, I never got your PM. |
|
|
| Report Abuse |
|
|
|
| 29 Dec 2011 06:33 PM |
| Oh dear. That's not good. It probably got auto-filtered like my post. |
|
|
| Report Abuse |
|
|
|
| 29 Dec 2011 06:36 PM |
function InsertCommas ( number ) number = tostring(number) number = number:reverse() number = number:gsub("%d%d%d", function(a) return a .. "," end) number = number:reverse() number = number:sub(number:find("%d"), -1) return number end
print(InsertCommas(1000000)) -- 1,000,000 |
|
|
| Report Abuse |
|
|
|
| 29 Dec 2011 06:37 PM |
Phew. Got it through.
~Floodcheck~ |
|
|
| Report Abuse |
|
|
|
| 29 Dec 2011 06:42 PM |
| Crazyman32 made one too :P |
|
|
| Report Abuse |
|
|
Smirky
|
  |
| Joined: 05 Apr 2008 |
| Total Posts: 3598 |
|
|
| 29 Dec 2011 06:48 PM |
| Woah! Is that the simplest way to do it? |
|
|
| Report Abuse |
|
|
|
| 29 Dec 2011 08:39 PM |
This is the one from my model:
function addComma(n) local f,k = n while (true) do f,k = string.gsub(f,"^(-?%d+)(%d%d%d)","%1,%2") if (k == 0) then break end end return f end
-- Example: print(addComma(100000.25))
No, I'm not a string manipulator genius. I found that part on google :P |
|
|
| Report Abuse |
|
|
DXPower
|
  |
| Joined: 21 Oct 2008 |
| Total Posts: 2866 |
|
| |
|