在lua中舍入复数和实数

时间:2019-07-24 08:17:09

标签: function lua arguments rounding

我想要一个用于复数的舍入函数。假定复数将始终以a + bi形式写入,其中a和b是实数。我想要一个类似于以下功能的功能。

function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

此函数舍入实数。我到底想要什么。

 round(2.784,2) --will result into 2.78
    round(2.78655,3) --will result into 2.787
    round(2.784+3.1245i,2) --will result into 2.78+3.12i
    round("2.7885+3.1246i",3) --will result into 2.789+3.125i
    round("3.1246i",2) --will result into 3.12i

0 个答案:

没有答案