I am adding integers and floats numbers in javascript using a single function, I saw various solution but I came up the following solution using "number" function which was able to handle both integers and floats.
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HandelNullValues.aspx.cs"
Inherits="HandelNullValues" %>
<!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>
<title>Add Numbers</title>
<script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function AddNumbers(PrmFirstNumber, PrmSecondNumber) {
if (isNaN(PrmFirstNumber) || isNaN(PrmSecondNumber)) {
return NaN;
}
else {
return Number(PrmFirstNumber) + Number(PrmSecondNumber);
}
}
$(document).ready(function () {
$("#BtnAddNumber").click(function () {
var FirstNumber = $("#TextFirstNumber").val();
var SecondNumber = $("#TextSecondNumber").val();
alert(AddNumbers(FirstNumber, SecondNumber));
alert("for precision: " + AddNumbers(FirstNumber, SecondNumber).toFixed(2));
});
});
</script>
</head>
<body style="font-family: Arial; font-size: 100%;">
<div>
<table cellpadding="5" cellspacing="0" style="border: none 0px transparent;">
<tr>
<td>
<label>
First Number</label>
</td>
<td>
<input type="text" maxlength="3" id="TextFirstNumber" />
</td>
</tr>
<tr>
<td>
<label>
Second Number</label>
</td>
<td>
<input type="text" maxlength="3" id="TextSecondNumber" />
</td>
</tr>
<tr>
<td colspan="2">
<button id="BtnAddNumber" type="button">
Add</button>
</td>
</tr>
</table>
</div>
</body>
</html>