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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

counting strings

Status
Not open for further replies.

jscorpion

Programmer
Nov 17, 2000
40
US
Sorry for bothering everone again, but I have two more question. I was wondering how to take a number and start from the far right and read one number or decimal spot at a time. I am wanting to take a reading from an outside source then take each digit, locate it in a case statement then write out the appropriate jpg. I am able to do this in vb by creating a while loop.

while i<5
i = i + 1
singledigit = right$(number,1)
call sub casestatement()
wend

I am wandering how to do basically the same thing in javascript. I can't seem to find out how to pick apart a string. I also do not know how to format a number in javascript. In vb it is &quot;Label1.Caption = Format(numbervar, &quot;######.00&quot;)&quot;. If someone could help me with this I would deeply appreciate it. Like I mentioned the last time, I am new to javascript and I can't seem to find this info in the books that I am referencing.

Thank you,

jscorpion
 
no problem j... I'll answer your first question quickly...

to access different pieces of a string (loop through it char by char) you can use:

for(var i=0;i<mystring.length;i++)
{
alert(mystring.charAt(i))
}

that will alert each letter, one at a time. Of course, you probably don't need to alert the letters, so change that part to whatever you need.

Also to actually get a larger piece of a string, use the substring method. more on all the string methods here:


There's no built in way to get a number to format that way, but you can try working with:

<script>
d=5.111111
</script>
<button onClick=&quot;alert(d.toFixed(2))&quot;>test</button> jared@aauser.com
 
the toFixed() method only works in ie.

but, if you are trying to format a number to US currency, you can use this:

<script>
function String_toArray()
{
var charAr = new Array();
for(var i = 0;i < this.length;i++)
{
charAr = this.charAt(i);
}
return charAr;
}
String.prototype.toArray = String_toArray;

function String_reverse()
{
var str, charAr;
charAr = new Array();
charAr = this.toArray();
charAr = charAr.reverse();
str = charAr.join(&quot;&quot;);
return str;
}
String.prototype.reverse = String_reverse;

function Number_toUSCurrency()
{
var str, tmpsplit, tmp, c, t, x;
str = new String(this);
tmpsplit = new Array();
if(str.indexOf(&quot;.&quot;) > -1)
{
tmpsplit = str.split(&quot;.&quot;);
if(tmpsplit[1].length < 2)
{
tmpsplit[1] += &quot;0&quot;;
}
else if(tmpsplit[1].length > 2)
{
t = new Array();
t[0] = tmpsplit[1].substring(0, 1);
t[1] = new Number(tmpsplit[1].substring(1, 2));
t[3] = tmpsplit[1].substring(2, 3);
if(parseInt(t[3]) > 5)
{
t[1]++;
}
t[1] = new String(t[1]);
tmpsplit[1] = t[0]+t[1];
}
}
else
{
tmpsplit[0] = str;
tmpsplit[1] = &quot;00&quot;;
}
if(tmpsplit[0].length > 3)
{
c = 0;
x = 1;
tmpsplit[0] = tmpsplit[0].reverse();
tmp = new Array();
for(var i=0;i<((Math.floor(tmpsplit[0].length/3))+tmpsplit[0].length); i++)
{
if((!(x%4)))
{
tmp = &quot;,&quot;;
}
else
{
tmp = tmpsplit[0].charAt(c);
c++;
}
x++;
}
tmpsplit[0] = tmp.join(&quot;&quot;);
tmpsplit[0] = tmpsplit[0].reverse();
}
return &quot;$&quot;+tmpsplit.join(&quot;.&quot;);
}
Number.prototype.toUSCurrency = Number_toUSCurrency;
</script>

<script>
var d = 23.765;
alert(d.toUSCurrency()); adam@aauser.com
 
uh-oh.. i for got to take off tgml...

sorry. code above = bad. this will work:

<script>
function String_toArray()
{
var charAr = new Array();
for(var i = 0;i < this.length;i++)
{
charAr = this.charAt(i);
}
return charAr;
}
String.prototype.toArray = String_toArray;

function String_reverse()
{
var str, charAr;
charAr = new Array();
charAr = this.toArray();
charAr = charAr.reverse();
str = charAr.join(&quot;&quot;);
return str;
}
String.prototype.reverse = String_reverse;

function Number_toUSCurrency()
{
var str, tmpsplit, tmp, c, t, x;
str = new String(this);
tmpsplit = new Array();
if(str.indexOf(&quot;.&quot;) > -1)
{
tmpsplit = str.split(&quot;.&quot;);
if(tmpsplit[1].length < 2)
{
tmpsplit[1] += &quot;0&quot;;
}
else if(tmpsplit[1].length > 2)
{
t = new Array();
t[0] = tmpsplit[1].substring(0, 1);
t[1] = new Number(tmpsplit[1].substring(1, 2));
t[3] = tmpsplit[1].substring(2, 3);
if(parseInt(t[3]) > 5)
{
t[1]++;
}
t[1] = new String(t[1]);
tmpsplit[1] = t[0]+t[1];
}
}
else
{
tmpsplit[0] = str;
tmpsplit[1] = &quot;00&quot;;
}
if(tmpsplit[0].length > 3)
{
c = 0;
x = 1;
tmpsplit[0] = tmpsplit[0].reverse();
tmp = new Array();
for(var i=0;i<((Math.floor(tmpsplit[0].length/3))+tmpsplit[0].length); i++)
{
if((!(x%4)))
{
tmp = &quot;,&quot;;
}
else
{
tmp = tmpsplit[0].charAt(c);
c++;
}
x++;
}
tmpsplit[0] = tmp.join(&quot;&quot;);
tmpsplit[0] = tmpsplit[0].reverse();
}
return &quot;$&quot;+tmpsplit.join(&quot;.&quot;);
}
Number.prototype.toUSCurrency = Number_toUSCurrency;
</script>

<script>
d = 34500.765;
alert(d.toUSCurrency());
</script> adam@aauser.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top