323 posts
Location
Boston, MA
Posted 14 August 2014 - 01:55 AM
So, I'm programming a sign. Amazing right?
I am printing the time of the world on that sign, but it prints something along the lines of "14.582" (basically, it prints 3 digits after the period).
Is there any way to make it so that it prints "14.6" (1 digit after the period)?
Any help is much appreciated.
3057 posts
Location
United States of America
Posted 14 August 2014 - 02:07 AM
math.ceil() rounds to the nearest integer (up),
math.floor() rounds down. I would write something like this (note - I have not tested this):
function round( num, place ) --#num is the decimal (eg. 14.582), place is the decimal place (eg 1)
local wNum = num * ( 10^place ) --#make it an integer
local low = math.floor( wNum ) --#find the high/low round
local high = math.ceil( wNum )
local lDiff = math.abs( wNum - low ) --#find the difference
local hDiff = math.abs( high - wNum )
local minDiff = math.min( lDiff, hDiff ) --#find the lowest difference
if minDiff == hDiff then --#return the correct number
return high / (10^place)
else
return low / (10^place)
end
end
Edited on 14 August 2014 - 12:09 AM
7508 posts
Location
Australia
Posted 14 August 2014 - 02:17 AM
while KingofGamesYami's solution does work, it is very verbose, a simpler solution would be to do the following
local function round( num, idp )
local mult = 10^(idp or 0)
if num >= 0 then
return math.floor(num * mult + 0.5 ) / mult
end
return math.ceil(num * mult - 0.5) / mult
end
3057 posts
Location
United States of America
Posted 14 August 2014 - 02:23 AM
while KingofGamesYami's solution does work, it is very verbose, a simpler solution would be to do the following
local function round( num, idp )
local mult = 10^(idp or 0)
if num >= 0 then
return math.floor(num * mult + 0.5 ) / mult
end
return math.ceil(num * mult - 0.5) / mult
end
…wow
7083 posts
Location
Tasmania (AU)
Posted 14 August 2014 - 03:15 AM
… and that's without a ternary. ;)/>
7508 posts
Location
Australia
Posted 14 August 2014 - 03:21 AM
using the ternary operator in this instance, while making it more concise, would definitely decrease readability…
108 posts
Posted 14 August 2014 - 05:54 AM
while KingofGamesYami's solution does work, it is very verbose, a simpler solution would be to do the following
local function round( num, idp )
local mult = 10^(idp or 0)
if num >= 0 then
return math.floor(num * mult + 0.5 ) / mult
end
return math.ceil(num * mult - 0.5) / mult
end
could it not just be this?
local function round(num,idp)
local mult=10^(idp or 0)
return math.floor(num * mult + 0.5 ) / mult
end
7083 posts
Location
Tasmania (AU)
Posted 14 August 2014 - 06:45 AM
Depends whether the time ever goes negative, I suppose.
108 posts
Posted 14 August 2014 - 07:56 AM
I only see a difference when the fractional part of num is 0.5, at which point as far as I can tell rounding is allowed to go either way
1140 posts
Location
Kaunas, Lithuania
Posted 14 August 2014 - 11:10 AM
There is a built-in function called
textutils.formatTime wich converts the output from os.time to a human-readable format, for example, if os.time returns 14.5 then textutils.formatTime may produce 14:30 or 2:30 pm if you tell it to use 12-hour format.