-2

The number is a single digit or whatever its, I want the value as before 2 digit and after 3 digits in decimal value.

Received values:
7, 
07, 
07.1, 
07.10, 
17.222,

Expected output:

07.000, 07.000, 07.100, 07.100, 17.222

Muthukumar
  • 27
  • 4
  • 1
    duplicates: [How can I pad a value with leading zeros?](https://stackoverflow.com/questions/1267283/how-can-i-pad-a-value-with-leading-zeros) and [Format number to always show 2 decimal places](https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places) – pilchard Dec 28 '22 at 10:23
  • function f_(n){ n = n ? n.split('.') : []; if( !n[0] ){ n[0] = '00'; } if( !n[1] ){ n[1] = '000'; } for(let a=1; a>-1; a--){ if( !n[0][a] ){ n[0] = '0'+n[0]; } } for(let a=0; a<3; a++){ if( !n[1][a] ){ n[1] += '0'; } } return n.join('.'); } for(let x of ['','7', '07', '07.1', '07.10', '17.222']){ console.log( f_(x) ) } – Hanna Rose Dec 28 '22 at 10:55

1 Answers1

0
function formatNumber(number) {
  return number.toFixed(3).padStart(6, '0');
}

Does this do the trick for you ?

Johan
  • 2,088
  • 2
  • 9
  • 37