Solved

looking for a similar function as parseInt

Posted on 2004-09-09
5
649 Views
Last Modified: 2008-03-10
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
0
Comment
Question by:DesertWarrior
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 36

Assisted Solution

by:Zyloch
Zyloch earned 250 total points
ID: 12022286
Hi

Basically, you just use parseInt (or parseFloat) and then check if the resulting value isNaN or not:

function getInt(int,base) {
   base=(base) ? parseInt(base,10) : 10;
   if (isNaN(base)) {
      base=10;
   }
   int=parseInt(int,base);
   if (isNaN(int)) {
      return(0);
   } else {
      return(int);
   }
}

Regards,
Zyloch
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 12022581
if you simply want to assign a 0 to '1.#QNAN' :

var theVal = isNaN(num) ? 0 : num;

Vinny

0
 
LVL 15

Accepted Solution

by:
Thogek earned 250 total points
ID: 12022583
Actually, as long as the argument to parseInt begins with a numeric character, parseInt will return an integer.  For example, parseInt('1.#QNAN') == 1.  (See http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthparseint.asp.)

You could, however, do something like

    function TransToInt(strVal) {
        if(!strVal)
            return 0;
        var intVal = parseInt(strVal);
        if(isNaN(intVal)  ||  '' + intVal != strVal)
            return 0;
        else
            return intVal;
    }
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12025988
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>



0
 
LVL 9

Expert Comment

by:cwolves
ID: 12030617
<script>
var x = 'asdf';
var i = isNaN(parseInt(x))?0:parseInt(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>
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question