Solved

looking for a similar function as parseInt

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now