0

Currently this code outputs a string in two decimal places and I need it to be rounded to the closest integer. I had a play around with "math.round" but was unable to get it to work. Any assistance on this would be greatly appreciated!

function myFunction() {
  var str = document.getElementById("blog-body").innerHTML;
  var n = str.match(/(\w+)/g).length;
  var x = n / 200;
  var y = x.toFixed(2);
  document.getElementById("result").innerHTML = y + ' Min |';
}

Thank you

  • Does this answer your question? [How to round to at most 2 decimal places, if necessary](https://stackoverflow.com/questions/11832914/how-to-round-to-at-most-2-decimal-places-if-necessary) – samnoon Jul 19 '22 at 04:05

1 Answers1

0

Use toFixed(0)

Rounding to nearest integer: (123.123).toFixed(0) results in 123

(123.89).toFixed(0) results in 124


You can also use Math.round(123.3) results in 123 and Math.round(123.89) results in 124

Lzh
  • 3,585
  • 1
  • 22
  • 36