# 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```

Try using modulo operator,then round the output to int type number

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

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

i want to make it  like this

1. function hoursToDays(hours)
2.     local totalHours =(hours)
3.     if totalHours then
4.         local hours =.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

14 hours ago, TheMOG said:

i want to make it  like this

1. function hoursToDays(hours)
2.     local totalHours =(hours)
3.     if totalHours then
4.         local hours =.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

• 1

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_

Can't edit original post. Doublepost ftw!

```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

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

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

So i just realised i dont need this. But thanks anyone!