itHyperoX

HoursToDay

Recommended Posts

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

 

Share this post


Link to post
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

Share this post


Link to post
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 )

 

Share this post


Link to post

i want to make it  like this

 

  1. function hoursToDays(hours)
  2.     local totalHours = tonumber(hours)
  3.     if totalHours then
  4.         local hours = math.floor(totalHours/60)
  5.         local hours = totalHours - hours*60
  6.         if hours and hours then
  7.             return hours,hours
  8.         else
  9.             return 0,0
  10.         end
  11.     end
  12. end

Share this post


Link to post
14 hours ago, TheMOG said:

i want to make it  like this

 

  1. function hoursToDays(hours)
  2.     local totalHours = tonumber(hours)
  3.     if totalHours then
  4.         local hours = math.floor(totalHours/60)
  5.         local hours = totalHours - hours*60
  6.         if hours and hours then
  7.             return hours,hours
  8.         else
  9.             return 0,0
  10.         end
  11.     end
  12. 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 :)

 

  • Like 1

Share this post


Link to post

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 by LopSided_

Share this post


Link to post

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

Share this post


Link to post

Why are you dividing by 60 @TheMOG, 1 day = 24 hours, so you divide by 24

Share this post


Link to post

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 by MrTasty

Share this post


Link to post

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.