View Full Version : Converting String to Numeric
Hi All,
I was trying to insert data into a Sybase Database, and it gave me conversion error, unable to convert varchar to numeric.
How can I convert the string value to numeric from the textbox i.e
from the <input type=text ...............
Thanks all
Abdul
This should help. Convert the input values to numbers using either
parseInt(str)//integer
parseFloat(str)//floating point
Integer gives whole numbers and floating point gives decimals. Here is an example using the parseInt().
<html>
<head>
<title>Example</title>
</head>
<script language="javascript" type="text/javascript">
<!-- ;
function convertToNumber(){
var str1=document.forms[0].t1.value;
var str2=document.forms[0].t2.value;
var a=parseInt(str1);
var b=parseInt(str2);
var c=a+b;
alert(a +" + " + b +" = "+c);
}
// -->
</script>
<body>
<form>
<h4>Enter a number in each textbox</h4>
<input type="text" name="t1" />A
<br>
<input type="text" name="t2" />B
<br>
<input type="button" value="Send" onclick="convertToNumber()"/>
</form>
</body>
</html>
Not sure if it still won't be a varchar though.
hth
adios
09-19-2002, 04:02 PM
http://developer.netscape.com/docs/manuals/js/server/jsguide/data.htm
http://caucuscare.com/~roth/JAVASCRIPT/ch09_02.htm
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.