Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ Programming
➜ General
➜ Format number to use comma in Lua?
Format number to use comma in Lua?
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Darwin
USA (125 posts) Bio
|
Date
| Mon 17 Mar 2008 03:39 AM (UTC) |
Message
| I was wondering if there was a simple way to format long numbers (4 or more digits) to use commas in Lua. The PiL book said to refer to a C manual, but C suggests using "%'d" which does not work in Lua.
> print(string.format("%'d", 5000))
stdin:1: invalid option '%'' to 'format'
stack traceback:
[C]: in function 'format'
stdin:1: in main chunk
[C]: ?
Any suggestions? | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #1 on Mon 17 Mar 2008 03:48 AM (UTC) |
Message
| input = arg[1]
input2 = string.gsub(input, "(%d)(%d%d%d)$", "%1,%2", 1)
while true do
input2, found = string.gsub(input2, "(%d)(%d%d%d),", "%1,%2,", 1)
if found == 0 then break end
end
print(input)
print(input2)
And running it:
$ lua formatter.lua 100000000
100000000
100,000,000
You need the while loop because it seems that gsub only repeats if you do left-to-right substitution, but this is more of a right-to-left substitution. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #2 on Mon 17 Mar 2008 04:03 AM (UTC) |
Message
| Oh, and note that %'d doesn't work because it's not in the C standard. 'man printf' indicates that many C runtime libraries are not aware of the ' specifier and will fail to handle it. Since Lua talks straight to the C library in this case, if your C library doesn't handle it then Lua won't either. (That's why Lua refers you to the C manual.) |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Darwin
USA (125 posts) Bio
|
Date
| Reply #3 on Mon 17 Mar 2008 04:11 AM (UTC) |
Message
| Thanks, David. Works like a charm. :) | Top |
|
Posted by
| Nick Gammon
Australia (23,068 posts) Bio
Forum Administrator |
Date
| Reply #4 on Mon 17 Mar 2008 04:38 AM (UTC) |
Message
| |
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #5 on Mon 17 Mar 2008 04:44 AM (UTC) |
Message
| Oh, heh, I didn't know about that. I'd definitely recommend Nick's version instead: it handles more cases than my quick hack does, and has good explanations of what is going on. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
22,445 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top