FANDOM


This pair of functions allow you to pad a string (or number) to a given length with a specified character. This might be useful in situations where you need to pad a number with leading zeroes for example.

Usage:

padLeft(24, 5, '0') // Returns '00024'
padRight('Foo', 10, 'b') // Returns 'Foobbbbbbb'

function padLeft(val, len, repChar) {
	var strReturn = val.toString();
	var x;
	for (x = 1; x <= num - val.toString().length; x++) {
		strReturn = repChar.toString() + strReturn;
	}
	return strReturn;
}

function padRight(val, len, repChar) {
	var strReturn = val.toString();
	var x;
	for (x = 1; x <= len - val.toString().length; x++) {
		strReturn += repChar.toString();
	}
	return strReturn;
}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.