JSFiddle - React, Tailwind, and code Playground

by brigand

HTML

<pre id="out"></pre>

JavaScript

function trimFixed(string) {
  return string.replace(/\.0+$|(\.[^0]*)0+/, '$1');
}

function toFixedTrim(number, places) {
  return trimFixed(number.toFixed(places));
}

out.textContent = [
  1,
  1.2,
  1.23,
  1.234,
  1.2345,
  1.23456,
].map(n => toFixedTrim(n, 4)).join('\n')

out.textContent += '\n\nMath:\n';
out.textContent += [
	4.355555
].map(n => Math.floor(n * 10000) / 10000).join('\n')