This shows you the differences between two versions of the page.
— |
function_round [2018/08/18 11:14] (current) Niek created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <file lua round.lua> | ||
+ | -- Copyright (C) 2018 CurlyMo & Niek | ||
+ | -- This Source Code Form is subject to the terms of the Mozilla Public | ||
+ | -- License, v. 2.0. If a copy of the MPL was not distributed with this | ||
+ | -- file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
+ | |||
+ | |||
+ | local M = {} | ||
+ | |||
+ | local function round(num, numDecimalPlaces) | ||
+ | local mult = 10^(numDecimalPlaces or 0) | ||
+ | return math.floor(num * mult + 0.5) / mult | ||
+ | end | ||
+ | |||
+ | function M.run(a, b, c) | ||
+ | |||
+ | if a == nil or b == nil or c ~= nil then | ||
+ | error("ROUND requires two arguments"); | ||
+ | end | ||
+ | |||
+ | if tonumber(a) == nil then | ||
+ | error(string.format("ROUND argument #1 expected number, \"%s\" given", type(a))); | ||
+ | end | ||
+ | |||
+ | if tonumber(b) == nil then | ||
+ | error(string.format("ROUND argument #2 expected number, \"%s\" given", type(b))); | ||
+ | end | ||
+ | |||
+ | local n = tonumber(b); | ||
+ | if n < 0 or n ~= math.floor(n) then | ||
+ | error(string.format("ROUND argument #2 expected positive integer number, \"%s\" given", b)); | ||
+ | end | ||
+ | |||
+ | return round(a, b); | ||
+ | end | ||
+ | |||
+ | |||
+ | |||
+ | function M.info() | ||
+ | return { | ||
+ | name = "ROUND", | ||
+ | version = "1.0", | ||
+ | reqversion = "8.1.1", | ||
+ | reqcommit = "0" | ||
+ | } | ||
+ | end | ||
+ | |||
+ | return M; | ||
+ | </file> |