Description:

Welcome to Jqueryjohn.com.

In this article, I will explain how to perform typecasting using Jquery.  

In JQuery, the following type conversions are possible.

1. Integer to String:

Integer values can be converted to string by using toString() method.

var a = 10;
var b = a.toString();
alert(b);

The code returns string value “10”.

2. String to Integer:

To convert a value from string to Integer, use parseInt() method.

var a = "10.525";
var b = parseInt(a);
alert(b);

The code returns the integer value 10.

3. String to Float:

To convert a value from string to Float, use parseFloat() method.

var a = "10.5251";
var b = parseFloat(a).toFixed(2);
alert(b);

The above code returns the Float value 10.53. toFixed() method used to reduce the number of decimal values.

4. Float to Integer:

The below code explains how to convert float values to Integer by using parseInt() method.

var a = 10.525;
var b = parseInt(a);
alert(b);

The code returns the Integer value 10.

5. Float to String:

The below code explains how to convert Float values to String by using toString() method.

var a = 10.525;
var b = a.toString();
alert(b);

The code returns the String value “10.525”

6. Casting to Boolean:

Boolean() method returns true when a value with at least one character, other than zero, or an object.

Boolean() method returns false when a value is an empty string, number zero, undefined or null.

var a = '';
var Isvalid = Boolean(a);
alert(Isvalid);

The above code returns false.


type conversion in jquery, typecasting in jquery, data type conversion in javascript, jquery type conversion

1 comments:

  1. Handy, but none of this is jQuery, it's all plain javascript :)

    ReplyDelete

 
Top