Click to See Complete Forum and Search --> : Interger to String.


urxlnc
01-28-2003, 08:27 AM
Hi, I am a newbie to JavaScript, I have serious problems with converting integer to string.

I have interger which I want in zeros before format (Eg. 001 for 1)

I am using the following function for this, but I am getting errors, myinteger undefined.

function setZ(num){
var myinteger=num;
var mystring=myinterger + '';
var mystr;
if(num<=9) {
mystr='00'+ mystring;
}else{
if(num<=99){
mystr='0'+mystring;
} else {
mystr=mystring;
}}
return mystr;
}


Also can anyone tell me the syntax for CASE statment in JavaScript?

Help.

khalidali63
01-28-2003, 08:34 AM
Your function looks ok at a glance,make sure that this function is passed a valid number.

switch/case statement is used just as in Java or C/C++

switch(variable){
case value:
break;
and so on
}

cheers

Khalid

urxlnc
01-28-2003, 08:38 AM
Can I check between values in CASE statement?
What would be the syntax for 1 to 10 and 11 to 20 and so on... ?

Thanks a Lot.

khalidali63
01-28-2003, 08:47 AM
if you meant that

case 5:
break;
case 9:
break;
case 19:
break;


then yes you can do that.Swithc and case is not as sensitive as its in Java or C/C++ there fore unlike getting only integers or chars in Java/C/C++ you can even compare words too.

cheers

Khalid

urxlnc
01-28-2003, 09:00 AM
no, I meant

case <5:
break;
case >5 <9:
break;
case >10 <19:
break;

something like using this.


if (x<=5){
y=1;}
else{
if(x<=10){
y=2;}
else{
if(x<=19){ y=3 ;}
else{
if(x<=29){
y=4;}
else{
if(x<=39){ y=5;}
else{
x=y;
}
}
}
}
}
}

khalidali63
01-28-2003, 09:09 AM
I don't think so,Even if you could why not use if else statement which is meant for this kind of comparisons?

Charles
01-28-2003, 09:24 AM
<script type="text/javascript">
<!--
Number.prototype.setZ = function () {switch (Math.floor(Math.LOG10E * Math.log(Number(this)))) {case 0: return '00' + this.toString(); case 1: return '0' + this.toString(); default: return this.toString()}}

alert(Math.PI.setZ());
// -->
</script>

urxlnc
01-28-2003, 09:27 AM
Because its very lengthy, nevermind.

well one more thing, I am using parseInt() function to get the interger value of String.

Now its working fine with few things and not working fine with the other.

For Example.
parseInt('001')=1
parseInt('002')=2
parseInt('003')=3
parseInt('004')=4
parseInt('005')=5
parseInt('006')=6
parseInt('007')=7
parseInt('008')=0
parseInt('009')=0
parseInt('010')=8
parseInt('011')=9

I am damn sure its have to do something with parseInt() because the output I am using as parseInt(x)+"abc"+x

At the last the x value is ok, but at the first the parseInt() is confusing.

What should I do ?

khalidali63
01-28-2003, 09:34 AM
did you mean it truncates the preceding zeros?

say if you pass a string
val = "001"
result of this

parseInt(val)+"abc"+val

will be

1abc001

is that what you meant?
if yes thenwhen you convert it to an integer it does not care for preceding zeros the only thing that matters is the ones on the right side.

Khalid

Charles
01-28-2003, 09:35 AM
I'm not sure what it is that you are trying to acomplish there but you don't, as a rule, need to call parseInt() directly. JavaScript automatically converts between data types.

urxlnc
01-28-2003, 09:52 AM
Yep, that is what I meant, I want the data in this format.
1abc001

but what I am getting with this is

parseInt(val)+'abc'+val=1abc001 (val='001')
parseInt(val)+'abc'+val=2abc002 (val='002')
parseInt(val)+'abc'+val=3abc003 (val='003')
parseInt(val)+'abc'+val=4abc004 (val='004')
parseInt(val)+'abc'+val=5abc005 (val='005')
parseInt(val)+'abc'+val=6abc006 (val='006')
parseInt(val)+'abc'+val=7abc007 (val='007')
parseInt(val)+'abc'+val=0abc008 (val='008')
parseInt(val)+'abc'+val=0abc009 (val='009')
parseInt(val)+'abc'+val=8abc010 (val='010')
parseInt(val)+'abc'+val=9abc011 (val='011')
parseInt(val)+'abc'+val=10abc012 (val='012')

Charles
01-28-2003, 09:57 AM
Does do it for you?

<script type="text/javascript">
<!--
Number.prototype.setZ = function () {switch (Math.floor(Math.LOG10E * Math.log(Number(this)))) {case 0: return this.toString() + 'abc00' + this.toString(); case 1: return this.toString() + 'abc0' + this.toString(); default: return this.toString() + abc + this.toString()}}

alert(Number(23).setZ());
// -->
</script>

urxlnc
01-28-2003, 10:04 AM
Now forget everything, look at the function below what do you think I am going wrong?

the Variable bkno that is being passed to the function is in the format "001"

function dofindlayer(volno,bkno,verno) {
var volumeno = parseInt(volno);
var verseno = parseInt(verno);
var bk = parseInt(bkno);
var text;
if(verseno<=9) {
text = bk +".html#00"+ volumeno +"."+ bkno +".00"+verseno;
} else {
if(verseno<=99){
text = bk +".html#00"+ volumeno +"."+ bkno +".0"+verseno;
} else {
text = bk +".html#00"+ volumeno +"."+ bkno +"."+verseno;
}}
setLayer(text);
}

Charles
01-28-2003, 10:07 AM
You are not being clear enough. What exactly do you want that piece of script to do? And look above a few posts. I've posted a method that formats your number the way that you have requested.

khalidali63
01-28-2003, 10:15 AM
Your right ur excelency.

if I do this

alert(parseInt("008") it gives me 0
...

hunmm..interesting..
I guess you woke me up..let me see I can come up with something..
lol

Khalid

Charles
01-28-2003, 10:16 AM
Is this what you are after?

<script type="text/javascript">
<!--
Number.prototype.padd = function () {switch (Math.floor(Math.LOG10E * Math.log(Number(this)))) {case 0: return '00' + this.toString(); case 1: return '0' + this.toString(); default: return this.toString()}}

function doFindLayer(volno,bkno,verno) {alert (bkno + '.html#00' + volno + '.' + bkno + Number(verno).padd())}

doFindLayer(1,2,3);
// -->
</script>

khalidali63
01-28-2003, 10:36 AM
use parseFloat(val)
instead.Its just a hack,Since I know you will not be using float anyways and it will always return an integer value.

Its interesting the way parseInt("008") will return 0.

cheers

Khalid

urxlnc
01-28-2003, 11:04 AM
Problem solved with parseFloat()

:D

Working too great now !! :cool: