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 resp_codeta 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:


<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: