toFixed

This is a safe way to round numbers to a given precision in javascript. Do not use toFixed because it will return unwanted results.

by tylorkolbeck

HTML

<h1>
  Safely set a number to a fixed position.
</h1>

JavaScript

// This is a safe way to round numbers to a given 
// precision in javascript.  Do not use toFixed because 
// it will return unwanted results. 

const toFixed = function(value, precision) {
  let exponentialForm = Number(value + 'e' + precision)
  let rounded = Math.round(exponentialForm)
  let finalResult = number(rounded + 'e-' + precision)
  return finalResult
}