215 posts
Location
Netherlands
Posted 11 August 2013 - 07:58 AM
Hello everyone,
I am doing Economy in school and you often divide millions and billions by dots. Like this
4321598 = 4.321.598
4935491364 = 4.935.491.364
Is there a way to do this in ComputerCraft? I know how you can split a string in to individual letters (Thanks to the tutorial of a user named Kingdaro), but how would you put the dots after specific numbers?
7508 posts
Location
Australia
Posted 11 August 2013 - 08:21 AM
Yes it is definitely possible…
Via performing
this Google search I was able to come up with the following
thread created by a person on the lua-users wiki which contains some code to do exactly this.
The code does use Lua patterns, so if you're unfamiliar with those I suggest that you have a read into them
here.
331 posts
Posted 11 August 2013 - 08:22 AM
i would advise that you check out the string API but,
it is possible so do this ( if you want it to split after every 3 numbers )
number = "4321567" -- you want 4.321.567 right? also make sure to make it a string
rep = math.floor(#number / 3) -- get how many dots to make
place = 0
if #number%3 ~= 0 then -- this gets what spot to put . in
place = #number%3 -- sets the place
end
spot = 0+place -- spot to insert
for i = 1, rep do
sub1 = number:sub(1, spot) -- seperate start of string to spot(were dot goes)
sub2 = number:sub(spot+1, #number) -- seperate spot(were dot goes) to end
number = sub1.."."..sub2 -- combine into new string
spot = spot + 4 -- next spot
end
print(number) -- print your number :)/>/>/>
i have no idea if this can be made more efficent this is just how logically i thought to do it and it seems to work with no errors,
only took me about 5mins and thought i would have trouble with numbers like 4211, so any amount of digit numbers work
Edit: OH NO! I got ninja'd while creating the program :/ hope you use mine :P/>