JSFiddle - React, Tailwind, and code Playground

by fergmux

JavaScript

// The function
const addZeroes = num => Number(num).toFixed(Math.max(num.split('.')[1]?.length, 2) || 2)


// Log test results
const inputs =['1', '2.0', '3.4', '4.56', '5.678', '6.780']
const expected =['1.00', '2.00', '3.40', '4.56', '5.678', '6.780']

inputs.forEach((input, index) => console.log(
	`input: ${input}`,
  `expected: ${expected[index]}`,
  `result: ${addZeroes(input)}`,
  `match: ${expected[index] === addZeroes(input)}`
  )
)