Solved

looking for a similar function as parseInt

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

810 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