Recent Posts
- Quick Code: Get the domain name in JS
- Things to think about when designing a logo
- Javascript Array Functions
- Integrating FCKEditor to save current content with AJAX using PHP
- Pad A Javascript Number
- Why use heading tags as opposed to font tags for displaying text in HTML?
- Creating One Time Download Links
- Why Javascript in Forms is Bad
- Five Things To Remember When Designing Accessible Web Pages
- Sizing Images Correctly – Part Two
Topics
Pad A Javascript Number
Posted on 26th March 2009 by Jacob
When doing a lot of front end scripting with Javascript you often find a lot of useful functions missing and so you create your own.
To keep design and content consistent I often pad numbers with zeros to ensure they are all the same width. There is no built in function to do this, so I wrote my own:
function pad(numNumber, numLength){
var strString = '' + numNumber;
while(strString.length<numLength){
strString = '0' + strString;
}
return strString;
}
Enjoy!
Be the first to add your voice to this post!
Start the discussion and leave your views or thoughts by writing a comment using the form above.