Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

seperating unknown characters in string? 1

Status
Not open for further replies.

Ovatvvon

Programmer
Feb 1, 2001
1,514
US
How would I take a string and not knowing the dynamic length, insert characters at specific spots. For example, say there is a counter on a web site that counts how many visitors have hit the page and add's one to the entry in the database. Now it's displayed on the page saying 1, 2, 3, 4, 5, 6, etc. What about when it hit's 1000 and I want to display a comma after the 1 so it is like "1,000", but stays like that when it hits 10,000, and what about a million so it is like: 1,000,000?

How can one specify the exact area's the commas are shown for any givin amount? -Ovatvvon :-Q
 
Do you really need a custom function that could handle anything, or would

formatNumber(someNumber)

do it for you? That will insert those commas for you.

Could work something up if you need, but just curious if that might solve your issue.

:)
paul
penny.gif
penny.gif
 
hmmm, learn somthin new every day. hee hee. That specific function format's it to what appears to be a currency format. Is there a format for numbers such as inserting the comma for every 3rd character? -Ovatvvon :-Q
 
Try this :

<%
dim strString
strString = &quot;10000000000&quot;
%>
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--
var strValue = '<%= strString %>'
var objRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
function addCommas() {
while(objRegExp.test(strValue)) {
strValue = strValue.replace(objRegExp, '$1,$2');
}
return strValue;
}
//-->
</script>


<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--
document.write(addCommas())
//-->
</script> Regards

Big Bad Dave

davidbyng@hotmail.com
 
formatNumber(number,0)

gets it to no decimal places with commas....

formatCurrency() will do money.

Is that function putting a dollar sign or something?
penny.gif
penny.gif
 
perfect again Link9,
no dollar sign,but was entering it so the number 26 was formatted like 26.00

just figured it was currency, but you're right, there wasn't a dollar sign.

Am gonna have to re-go-over all the format options in vbscript. Thanks again! -Ovatvvon :-Q
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top