How to convert String to Float and Float to String?

Converting String to Float

if you want to convert a value from string to float ,you have to use the function
parseFloat().

Example:-
parseFloat("4.222");      4.222
parseFloat("5bbb");     5
parseFloat("6e2");     600
parseFloat("bbb");     NaN (means "Not a Number")


Converting Integer/Float to String

Integer (int) or float value can be converted to string by using the function or method toString().

Example:-

var a = 3.22;
a.toString();     "3.22"
var a = 5;
a.toString();     5

Example Code:-

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>


<body>
<script type="text/javascript" >
var a = 24;
var b = 666;
var c = a.toString()+b;
document.write(" to String function "+c);
</script>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>





Result:-
to String function -> 24666

No comments:

Post a Comment

Plz Share your comments...