JavaScript Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Java-Script Tutorial
Introduction Introduction
Variables Variables
Operators Operators
Statements Statements
Loops Loops
Functions Functions
Objects Objects
Arrays Arrays
Cookies JS Cookies
Browser Objects Browser Objects
Form DOM Form DOM
Style Objects DOM Style Objects
Forums Ask Your Doubts
Scraps More about Javascript
Feedback Feedback
 

Type Conversion


Tutorials Javascript

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"); 5
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:





A Note
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.

Other Links

web hosting