This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
chardo440's profile picture

Having an issue getting this number to round?

Started by chardo440, 19 February 2014 - 12:43 AM
chardo440 #1
Posted 19 February 2014 - 01:43 AM
I've tried placing the function multiple places I cannot seem to figure this one out :/ any help?



function clear()
   term.clear()
   term.setCursorPos(1,1)
end
while true do
   clear()
	  print("text here")
   local event, param1 = os.pullEvent ("char")
	  if param1 == "y" then
   clear()
	  print("Text question")
   x = tonumber(io.read())
   break
	  elseif param1 == "n" then
   print("text again")
   sleep(3)
	  else
   clear()
   print("try again")
	  sleep(1.5)
   end
end

x = x/3
function rnd()
math.floor(x + .5)
end
rnd()
print(x)
theoriginalbit #2
Posted 19 February 2014 - 01:59 AM
Is there an error? if so, what does it say? if not, what happens, and what is meant to happen?

More information allows us to help you quicker, simply saying "it does not work" is not helpful.
chardo440 #3
Posted 19 February 2014 - 02:14 AM
Well like I said in the title the end number is not being rounded X. how the system is setup there is 3 processing plants and when you access the computer you can choose to smelt. so say you have to smelt 16 items between the 3 plants well it's coming out 5.3333 something I just want it to round up one. so I make sure everything is always smelted. hope that helps.
Edited on 19 February 2014 - 01:16 AM
theoriginalbit #4
Posted 19 February 2014 - 02:24 AM
I stated it simply to try and get you to make better threads in the future, better threads are paramount to getting fast and reliable responses!

Basically your problem here is you're not saving the result of the rounding back into the variable. So currently you're rounding it and the forgetting the new number immediately.
chardo440 #5
Posted 19 February 2014 - 02:26 AM
Yeah I hear you. I appreciate it. what would I have to do to save it. How would I reassign that new value to x?
theoriginalbit #6
Posted 19 February 2014 - 02:28 AM
you assign it the same way you assign any other variable…

x = math.floor(x + 0.5)
chardo440 #7
Posted 19 February 2014 - 02:32 AM
Haha sorry. I'm still pretty fresh to lua. never used the math floor and I don't know why I went full tard mode. Thanks man it's working now.
theoriginalbit #8
Posted 19 February 2014 - 02:33 AM
You're welcome. we all have those moments no matter how long we've been programming, and we all learn from them.
MKlegoman357 #9
Posted 19 February 2014 - 09:10 AM
…so say you have to smelt 16 items between the 3 plants well it's coming out 5.3333 something I just want it to round up one

If you want it to be the highest integer you should use math.ceil. It returns the highest integer to the input, so if you give it 5.3333 it will come out as 6.0 where if you gave that number to your round function it would round to 5.0.