Jump to content

Comparision


raghunathmitte

Recommended Posts

try{String quantity=parseInt("abc123");if(quantity == NaN)throw "quantity cannot be null";}catch(exception){ alert(exception);}i have the above code in a function when i try to convert the string into int parseInt() method returns NaN value but if condition is not executing, is there any other way to compare with NaN?? please help me out

Link to comment
Share on other sites

You don't even need to use try ... catch (which is memory-intensive):

var quantity = "abc123";if (!isNaN(quantity)) quantity = parseInt(quantity);else alert("Quantity is Not a Number!");

Link to comment
Share on other sites

parseInt cannot handle letter in front of the number, only behind the number.
Sure it can - as long as you are using a base that is greater than 10:
var str = "rundmc";var num = parseInt(str, 36);alert(num);

Link to comment
Share on other sites

Sure it can - as long as you are using a base that is greater than 10:
var str = "rundmc"; var num = parseInt(str, 36); alert(num);

somehow i doubt that's what the OP had in mind...and thinking a little more about it, you are wrong.in your example, "rundmc" is a number, so there once again are no letters in front of the number...
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...