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.
9 replies to this topic
#1
Posted 14 August 2014 - 12:55 AM
#2
Posted 14 August 2014 - 01: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 by KingofGamesYami, 14 August 2014 - 01:09 AM.
#3
Posted 14 August 2014 - 01: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
#4
Posted 14 August 2014 - 01:23 AM
theoriginalbit, on 14 August 2014 - 01:17 AM, said:
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
#5
Posted 14 August 2014 - 02:15 AM
... and that's without a ternary.
#6
Posted 14 August 2014 - 02:21 AM
using the ternary operator in this instance, while making it more concise, would definitely decrease readability...
#7
Posted 14 August 2014 - 04:54 AM
theoriginalbit, on 14 August 2014 - 01:17 AM, said:
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
#8
Posted 14 August 2014 - 05:45 AM
Depends whether the time ever goes negative, I suppose.
#9
Posted 14 August 2014 - 06: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
#10
Posted 14 August 2014 - 10: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.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











