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( "taptest" )
local roundup = require( "roundup" )

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

t()

Inspired by

See also