Solved

looking for a similar function as parseInt

Posted on 2004-09-09
5
648 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

751 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