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

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

t()

Inspired by

See also