如何使用字符串作为参数调用Math.round?

时间:2018-08-07 10:15:42

标签: typescript

我正在尝试使用此函数舍入到小数点

function round(value, decimals) {
    return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
  }

但是我收到错误消息

[ts] Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures.

1 个答案:

答案 0 :(得分:2)

首先将字符串转换为数字,例如:

function round(value, decimals) {
  return Number(Math.round(Number(value + 'e' + decimals)) + 'e-' + decimals);
  // ----------------------^^^^^^^----------------------^
}