round

function round( x, n ) --> res

Description

Rounds x to a specified number with n digits.

Parameters

x

The number that should be round.

n

The number of digits to which you want to round x.

Return Values

res

The rounded number.

Code

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

return round

Examples

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

t.is( 2.2, round( 2.15, 1 ) )
t.is( 2.1, round( 2.149, 1 ) )
t.is( -1.48, round( -1.475, 2 ) )
t.is( 20, round( 21.5, -1 ) )
t.is( 1000, round( 626.3, -3 ) )
t.is( 0, round( 1.98, -1 ) )
t.is( -100, round( -50.55, -2 ) )

t.is( 0, round( 0, 2 ) )
t.is( 0, round( 0, -2 ) )

t.done()

Inspired by

See also