Jump to content

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

 

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

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
Link to comment
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
Link to comment
  • Administrators

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_
Link to comment
  • Administrators

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

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.
×
×
  • Create New...