SMH
SMH

Reputation: 1099

How to call Math.round with string as parameter?

I am trying to use this function to round to decimals

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

But I am getting error message

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

Upvotes: 2

Views: 1249

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075219

Convert the string to a number first, for instance:

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

Upvotes: 4

Related Questions