roundup

function roundup( x, n ) --> res

Description

Rounds x up, away from 0.

Parameters

x

The number that should be rounded up.

n

The number of digits to which x should be rounded.

Return Values

res

lorem ipsum

Code

--ZFUNC-roundup-v1
local function roundup( x, n ) --> res
   if x > 0 then
      return math.ceil( x * math.pow( 10, n ) ) / math.pow( 10, n )
   else
      return math.floor( x * math.pow( 10, n ) ) / math.pow( 10, n )
   end
end

return roundup

Examples

local t = require( "tapered" )
local roundup = require( "roundup" )

t.is( 4, roundup( 3.2, 0 ) )
t.is( 77, roundup( 76.9, 0 ) )
t.is( 3.142, roundup( 3.14159, 3 ) )
t.is( -3.2, roundup( -3.14159, 1 ) )
t.is( 31500, roundup( 31415.92654, -2 ) )

t.done()

Inspired by

See also