|
Messages >>>
Modifies a number by adding commas after every third digit. For example, 123456789 is converted to 123,456,789. The script example shows this script as used on numbers of various sizes.
Add the below code to the <body> section of your page:
<script
language="javascript"
type="text/javascript">
function
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);
else
output+=
','
+
number.substring(mod
+
3
*
i,
mod +
3
*
i +
3);
}
return
(output);
}
else
return
number;
}
document.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>');
</script>
|
|