Zero Fill - C# and JQuery
I recently ran into an issue when trying to add a zip code to a MVC view within the CMS we develop websites. The CMS holds the zipcode as a decimal, which a funny thing happens when the first New York address gets added. The leading zeros disappear.
Since we were using this in a Google Maps implementation I ultimately needed this resolved in the C# View as well as the JQuery.
Below are the two implementations that I added to resolve this issue. (Wish I could have just changed the type to a string, but this is a close second).
Since we were using this in a Google Maps implementation I ultimately needed this resolved in the C# View as well as the JQuery.
Below are the two implementations that I added to resolve this issue. (Wish I could have just changed the type to a string, but this is a close second).
JQuery
function zeroFill(number, width) {
width -= number.toString().length;
if (width > 0)
return new Array(width + (/\./.test(number) ? 2 : 1)).join('0') + number
return number + ""; // always return a string
}
Razor (C#)
@location.ZipCode.ToString("00000")
Comments
Post a Comment