Denis Saidov Denis Saidov - 4 months ago 16
jQuery Question

Format 1000 to 10.00 with jQuery

I've been looking for on how to format:

1000
to
10.00


8900
to
89.00


10000
to
100.00

etc.

Meaning simply get the number and add a dot (.) 2 decimals before its end. I found this PHP way of doing it Formatting numbers from 1000 to 10.00 , but cannot get a way to it with jQuery. Would really appreciate the help here.

Answer

You can divide the number by 100 then use toFixed(2), like this:

function format(num) {
  return (num / 100).toFixed(2);
}

console.log(format(1000))
console.log(format(8900))
console.log(format(10000))