Edited by astonish01, 17 August 2014 - 02:12 PM.
How to put comas between valors in a function
#1
Posted 17 August 2014 - 01:49 PM
#2
Posted 17 August 2014 - 02:00 PM
#3
Posted 17 August 2014 - 02:10 PM
MKlegoman357, on 17 August 2014 - 02:00 PM, said:
im going to make a example with energy cells,
cell = peripheral.wrap("side")
monitor = peripheral.wrap("side")
while true do
energy = cell.getEnergyStored("direction")
monitor.clear()
monitor.setCursorPos("number,number)
m.write(energy.." ")
sleep(0.5)
end
i may have said function and it is variable and you may thought print is printing in a printer, but with print i meant the monitor thing. (if so, this helps)
Edited by astonish01, 17 August 2014 - 02:11 PM.
#4
Posted 17 August 2014 - 02:15 PM
a = 14 b = 54 c = 25 str = a..", "..b..", "..c print(str)RESULT
14, 54, 25
Hope this helps
/sEi
EDIT: I was writing my answer before you posted the post above. Seing the post i am even more confused in what you try to do!
Edited by sEi, 17 August 2014 - 02:19 PM.
#5
Posted 17 August 2014 - 02:24 PM
sEi, on 17 August 2014 - 02:15 PM, said:
a = 14 b = 54 c = 25 str = a..", "..b..", "..c print(str)RESULT
14, 54, 25
Hope this helps
/sEi
yeah, but its like, i just have energy, and its one valor, the energy is never still, so it needs to keep reading the energy levels, so the number would be 50000000, but after some usage the energy would be 25000000, and i want to do 25,000,000 (of course when its full it will be 5,000,000)
so i don't know how to divide energy(as in the code) in diferent values to put them between the comas, and also, if the energy drops to 7 characters or 6, is there a way to remove the extra coma tht will be there? (like, it drops to 050,000 instead of 00,050,000 and after it comes back to 50,000,000)
Edited by astonish01, 17 August 2014 - 02:27 PM.
#6
Posted 17 August 2014 - 02:29 PM
/sEi
#9
Posted 17 August 2014 - 04:39 PM
function comma(text)
local textTable = {}
for char in string.gmatch(text, ".") do
textTable[#textTable + 1] = char
end
local commaCount = math.floor(#text / 3)
if (#text % 3 == 0) then
commaCount = commaCount - 1
end
local base = #textTable - 2
for i = 0, commaCount - 1 do
table.insert(textTable, (base - (i * 3)), ",")
end
local commaText = ""
for k,v in pairs(textTable) do
commaText = commaText..v
end
return commaText
end
EDIT: fixed a bug where numbers beyond one billion weren't readable (commas in wrong places)
Edited by GamerNebulae, 18 August 2014 - 12:08 PM.
#10
Posted 17 August 2014 - 05:56 PM
local function comma_value(n)
local left, num, right = n:match("^([^%d]*%d)(%d*)(.-)$")
return left..(num:reverse():gsub("(%d%d%d)","%1,"):reverse())..right
end
Edited by Anavrins, 18 August 2014 - 09:50 PM.
#11
Posted 17 August 2014 - 07:13 PM
#13
Posted 18 August 2014 - 02:09 AM
function comma_value( num )
local formatted = num
repeat
formatted, replaced = formatted:gsub("^(-?%d+)(%d%d%d)", '%1,%2')
until replaced == 0
return formatted
end
You can take a look at the PIL on patterns to see what parts do, however the basic breakdown is- ^ from the start of the string
- () capture these characters
- ? the previous character/match is optional
- %d match a decimal numbers
- + one or more repetition
- %n the nth capture, i.e. %1 is the first capture
Give me all the numbers, except for the last three, from the start of the sting into the first capture, and the last three numbers into the second capture. then combine them back together with a comma separating them.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











