»
EnglishFrenchVietnamese

Print - Add Commas - JavaScriptBank.com

Full version: jsB@nk » Calculation » Add Commas
URL: http://www.javascriptbank.com/add-commas.html

Add Commas © JavaScriptBank.comModifies a number by adding commas after every third digit. For example, 123456789 is converted to 123,456,789. The script example shows this JavaScript as used on numbers of various sizes. And, it demonstrates a clever use of the modulus operator.

Full version: jsB@nk » Calculation » Add Commas
URL: http://www.javascriptbank.com/add-commas.html



JavaScript
<SCRIPT LANGUAGE="JavaScript"><!-- Original:  Martin Webb --><!-- Web Site:  http://www.irt.org --><!-- Beginfunction Comma(number) {number = '' + number;if (number.length > 3) {var mod = number.length % 3;var output = (mod > 0 ? (number.substring(0,mod)) : '');for (i=0 ; i < Math.floor(number.length / 3); i++) {if ((mod == 0) && (i == 0))output += number.substring(mod+ 3 * i, mod + 3 * i + 3);elseoutput+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);}return (output);}else return number;}//  End --></script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<SCRIPT LANGUAGE="JavaScript"><!-- Begindocument.write(Comma(1)+'<BR>');document.write(Comma(12)+'<BR>');document.write(Comma(123)+'<BR>');document.write(Comma(1234)+'<BR>');document.write(Comma(12345)+'<BR>');document.write(Comma(123456)+'<BR>');document.write(Comma(1234567)+'<BR>');document.write(Comma(12345678)+'<BR>');document.write(Comma(123456789)+'<BR>');document.write(Comma(1234567890)+'<BR>');//  End --></script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->