How to Zero-pad Float Point Numbers in Javascript and with limit on max number of digits

javascript, jquery, node.js, underscore.js

Solution

n.toFixed(5).substring(0, 7)

It will fail miserably for anything 100000 or greater.

Problem

How can a float be right padded with zeros such that - Number of digits after padding is 7 - Zeros are padded to the right of the decimal point `.` - Numbers with more than 7 digits are truncated from the right `.toFixed(6)` ignores the number of digits before the decimal point `.`. Input ``` 9.123 9.123456 6.12345678 100.1 ``` Output ``` 9.123000 9.123450 6.123456 100.1000 // truncated from the right ```

Original source