Category

Javascript

JavaScript Numbers

By | Javascript

What is JavaScript equivalent for number_format in php?


1
2
3
4
5
6
7
8
// Lodash to rescue
// Works exactly as we expect. Output is a typeof number.
_.round("10.608", 2); // 10.61

// Following returns the output as string. That's one major down side.
parseFloat("10").toFixed(2); // 10.00
parseFloat("10.60").toFixed(2); // 10.60
parseFloat("10.605").toFixed(2); // 10.61