DesertWarrior
asked on
looking for a similar function as parseInt
Hi
What function other than parseInt transforms a string into an integer? I'm using parseInt but It doesn't work with values like 1.#QNAN
I'm looking for a function that will transform anything that is not an integer into a 0 (zero)
thanks
What function other than parseInt transforms a string into an integer? I'm using parseInt but It doesn't work with values like 1.#QNAN
I'm looking for a function that will transform anything that is not an integer into a 0 (zero)
thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The best conversion from String to Number is by multiplicate by one. It converts eben empty string directly to zero.
Here an example:
<script>
var myNum1 = "1.#QNAN";
var res1 = myNum1*1;
if(isNaN(res1)) res1=0;
alert(res1)
</script>
Here an example:
<script>
var myNum1 = "1.#QNAN";
var res1 = myNum1*1;
if(isNaN(res1)) res1=0;
alert(res1)
</script>
<script>
var x = 'asdf';
var i = isNaN(parseInt(x))?0:parse Int(x);
</script>
if you want to convert stuff like '1asdf' into 1, try this:
<script>
var x = '1asdf';
var i = parseInt(x.replace(/[^0-9] +/ig,''));
i=isNaN(i)?0:i;
</script>
var x = 'asdf';
var i = isNaN(parseInt(x))?0:parse
</script>
if you want to convert stuff like '1asdf' into 1, try this:
<script>
var x = '1asdf';
var i = parseInt(x.replace(/[^0-9]
i=isNaN(i)?0:i;
</script>
var theVal = isNaN(num) ? 0 : num;
Vinny