rounddown

function rounddown( x, n ) --> res

Description

Rounds x down, toward zero.

Parameters

x

The number that should be rounded down.

n

The number of digits to which x should be rounded.

Return Values

res

The rounded number.

Code

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

return rounddown

Examples

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

t.is( 3, rounddown( 3.2, 0 ) )
t.is( 76, rounddown( 76.9, 0 ) )
t.is( 3.141, rounddown( 3.14159, 3 ) )
t.is( -3.1, rounddown( -3.14159, 1 ) )
t.is( 31400, rounddown( 31415.92654, -2 ) )

t.done()

Inspired by

See also