H I O X INDIA
Javascript Tutorial
 HOME  ||  Scripts  ||  Purchase  ||  Tutorials  ||  Images  ||  Tools  ||  Templates 
  :-)  Send Page   :-)   Feedback   :-)   Register   :-)   Links   :-)   Support   :-)
Español Français 中文 Deutsch Portuguese Japanese தமிழ்
 Forums   Hosting   Internet Stats   Easy Calculation   FUN Games 

Java-Script Tutorial
Introduction
Variables
Operators
Statements
Loops
Functions
Objects
Arrays
Browser Objects
Form DOM
Ask Your Doubts
More about Javascript
Feedback



Data Type Conversion


Topic

Converting a string data type to integer?
How to convert a float to integer in javascript?



Explanation

Data Type Conversion:
The following data type conversions are possible
a) String to Integer (int)
b) Float to Integer (int)
c) String to Float
d) Integer (int) to String
e) Float to String

a) Converting String to Integer
To convert a value from string to integer we have to use the function or method "parseInt(). Passing a correct argument to the function (e.g: "4") will return correct value else (e.g: "sd") 0 will be returned.

Example Result
parseInt("4"); 4
parseInt("5aaa"); 4
parseInt("4.33333"); 4
parseInt("aaa"); NaN (means "Not a Number")

b) Converting Float to Integer
To convert a float value in to integer we have to use the function or method parseInt.

Example Result
parseInt(5.133); 5

Example Code:
<script language="javascript">
var a = "334";
var b = 3;
var c = parseInt(a)+b;
var d = a+b;
document.write("parseInt(a)+b = "+c);
document.write(" a+b = "+d);
</script>


Result:





        Javascript (JS) is one of the most used languages in the world. Some times spelled as Java Script. Hope you enjoy this tutorial. Do send your feedback or suggestions on this javascript or java script tutorial. This is a copyright content.

privacy policy     license     sitemap
© 2004-2010 HIOX INDIA

Other Links