JSFiddle - React, Tailwind, and code Playground

by fergmux

HTML

<div id="results"/>

CSS

p {
  line-height: 8px;
}
li {
  list-style-type: none;
  margin-bottom: 30px;
}

JavaScript

// The function
function addZeroes(num) {
  const dec = num.split('.')[1]
  const len = dec && dec.length > 2 ? dec.length : 2
  return Number(num).toFixed(len)
}


// Display test results
const inputs =['', '1', '2.0', '3.4', '4.56', '5.678', '6.780']
const html = inputs.reduce((html, input) => {
	return html + `
  	<li>
			<p>Input: ${input}</p>
			<p>Output: ${addZeroes(input)}</p>
		</li>`
})
document.getElementById('results').innerHTML = html