Entry
How do I display the commas in numbers?
May 7th, 2003 04:08
Gaurav Sharma, Aaron Bennett, Gaurav Sharma
The format functon first removes the leading zeros by substringing the
part after the first zero is found.
This string is parsed in reverse and the delimiter (, or .) is placed
after spacing ( 2 or 3 )(lakh or million).
This string is again reversed to form the final output.
if ( i != 0)outstring += delimiter; makes sure that the delimiter
is not placed after the eos.
<HTML>
<HEAD>
<script language="javascript">
function format(value, spacing, delimiter)
{
var isplace =0;
var i;
var outstring = "";
// supress the leading 0's
var position;
for(i = 0; i < value.length ; i++)
{
if (value.charAt(i) != "0")
{
position = i;
break;
}
}
value = value.substring(i, value.length);
var len = value.length -1;
// inserts the delimiter in the number string in a reverse
manner.
for (i= len; i>=0; i--)
{
outstring += value.charAt(i);
isplace++;
if (isplace == eval(spacing))
{
if ( i != 0)outstring += delimiter;
isplace = 0;
}
}
// home grown reverse function
var len = outstring.length -1;
var output = "";
for (i= len; i>=0; i--)
{
output += outstring.charAt(i);
}
alert(output);
}
</script>
</HEAD>
<BODY>
<a href="javascript:format('00000100010000','3',',')">def</a>
</BODY>
</HTML>
The format function has following short comings,
1. right now the sign suffix will be also treated as a character
2 only integeral numbers are supported.
Hope this solves ur problem.
Gaurav Sharma