itHyperoX Posted July 16, 2017 Share Posted July 16, 2017 Hi, whats the problem? function hoursToDays(hours) local totalHours = tonumber(hours) if totalHours then local hours = math.floor(totalHours/60) local hours = totalHours - hours*60 if hours and hours then return hours,hours else return 0,0 end end end Link to comment
Protagonist95 Posted July 16, 2017 Share Posted July 16, 2017 Try using modulo operator,then round the output to int type number Link to comment
itHyperoX Posted July 16, 2017 Author Share Posted July 16, 2017 14 minutes ago, Protagonist95 said: Try using modulo operator,then round the output to int type number The problem is with the math i think, cuz no debugerror Link to comment
Protagonist95 Posted July 16, 2017 Share Posted July 16, 2017 33 minutes ago, TheMOG said: The problem is with the math i think, cuz no debugerror function hoursToDays(commandName, hour) local conv_hour = tonumber(hour) local day = math.floor(hour / 24) outputChatBox("Days: " .. tostring(day) .. " Hours: " .. tostring(conv_hour - (day * 24))) end addCommandHandler ( "tm", hoursToDays ) Link to comment
itHyperoX Posted July 16, 2017 Author Share Posted July 16, 2017 i want to make it like this function hoursToDays(hours) local totalHours = tonumber(hours) if totalHours then local hours = math.floor(totalHours/60) local hours = totalHours - hours*60 if hours and hours then return hours,hours else return 0,0 end end end Link to comment
Ayush Rathore Posted July 17, 2017 Share Posted July 17, 2017 14 hours ago, TheMOG said: i want to make it like this function hoursToDays(hours) local totalHours = tonumber(hours) if totalHours then local hours = math.floor(totalHours/60) local hours = totalHours - hours*60 if hours and hours then return hours,hours else return 0,0 end end end if you pass 360 in function or any other value result would be like this totalHours = 360 hours = (360/60) -- > 6 hours = 360 - (6*60) -- > 0 and btw i cant understand why you doing this 1 Link to comment
Administrators Lpsd Posted July 17, 2017 Administrators Share Posted July 17, 2017 (edited) To answer your original question about the problem with the code; once you have defined a local variable there's no need to declare its scope again within the same block. Simply change line 5 of your code to: hours = totalHours - hours*60 Moving on, your maths is completely wrong. See these examples. function hoursToDays(hours) if not hours then return false end local days = math.floor(hours/24) return days end Or if you want to be more specific: function round(num, numDecimalPlaces) local mult = 10^(numDecimalPlaces or 0) return math.floor(num * mult + 0.5) / mult end function hoursToDays(hours) if not hours then return false end local days = round(hours/24, 1) return days end Edited July 17, 2017 by LopSided_ Link to comment
Administrators Lpsd Posted July 17, 2017 Administrators Share Posted July 17, 2017 Can't edit original post. Doublepost ftw! Here's your original code, fixed: function hoursToDays(hours) local totalHours = tonumber(hours) if totalHours then local hours = math.floor(totalHours/60) hours = totalHours - hours*60 if hours then return hours else return 0,0 end end end FYI: this comes no where close to converting hours to days. I would suggest you use my examples Link to comment
pa3ck Posted July 17, 2017 Share Posted July 17, 2017 Why are you dividing by 60 @TheMOG, 1 day = 24 hours, so you divide by 24 Link to comment
Addlibs Posted July 17, 2017 Share Posted July 17, 2017 (edited) How about you just use these calculations: Full days: math.floor(hours / 24) Remaining hours: hours % 24 E.g. 60 hours would return as 2 full days (48 hr) and 12 remaining hours. Edited July 17, 2017 by MrTasty Link to comment
itHyperoX Posted July 17, 2017 Author Share Posted July 17, 2017 So i just realised i dont need this. But thanks anyone! Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now