Paul Rajs

Paul Rajs

  • NA
  • 641
  • 138.5k

How to Calculate also Decimals with in this Code in Jquery

Dec 7 2016 5:03 AM
hi Developers ,
i want to output like if i give 10+5+2.5-2.5=15
but in my code working with whole numbers only like if 1+2+3=5 , it will give correct answer .
but if am give 10+5+2.5+2.5=is not working so how can i get my expected output with this code.i have try a lot but i cant able to get exact output. so please help me to solve this error also suggest me if is possible with any other way.
<head id="Head1" runat="server">
<script src="../Tracking/js/jquery.js"></script>
<script>
function txtpercentage() {
var SumofTotal;
var Quantity = $('#txt2').val();
var strarray = Quantity.split(',');
var numericReg = /^[0-9.*+-/]+$/;
if (!numericReg.test(Quantity)) {
alert('Numeric characters only.');
}
else {
var str = Quantity;
var numbers = str.replace(/ /g, '').split(/[-+*\/]/g);
var operators = str.replace(/ /g, '').split(/\d*/g);
operators.shift();
var result = +numbers[0];
for (var i = 0; i < operators.length - 1; i++) {
result = eval(result + operators[i] + numbers[i + 1]);
if (isNaN(result)) {
$("#txt3").html(result.toFixed(2));
}
else {
$("#txt3").html(result.toFixed(2));
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<center>
<div>
<asp:TextBox ID="txt2" runat="server" onkeyup="txtpercentage()" AutoComplete="off"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator102" runat="server" ValidationExpression="^[0-9.*+-/]+$"
ValidationGroup="CsbEdit" ErrorMessage="Integer or Decimal Only !.." ControlToValidate="txt2" ForeColor="Red" Display="Dynamic" />
<asp:Label ID="txt3" runat="server" ForeColor="Red" Text="0.00"></asp:Label>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1"></asp:Button>
</div>
</center>
</form>
thanking you
Paul.S

Answers (8)