I’ve written a very simple jQuery function to return a string padded to a specified length, similar to the php equivalent str_pad.
1 2 3 4 5 6 7 8 | $.strPad = function(i,l,s) { var o = i.toString(); if (!s) { s = '0'; } while (o.length < l) { o = s + o; } return o; }; |
Example Usage:
$.strPad(12, 5); // returns 00012 $.strPad('abc', 6, '#'); // returns ###abc
This version only supports left padding, which is why it is labelled as only a simple version
.