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.
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));
}
}
}
}
ValidationGroup="CsbEdit" ErrorMessage="Integer or Decimal Only !.." ControlToValidate="txt2" ForeColor="Red" Display="Dynamic" />
thanking you
Paul.S
8 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Mangesh GPosted Dec 7, 2016, 9:30 AM
if (operators[i] === '.') {
operators.splice(i, 1);
}
}
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 = operators.length - 1; i >= 0; i--) {
if (operators[i] === '.') {
operators.splice(i, 1);
}
}
for (var i = 0; i < operators.length-1 ; i++) {
if (operators[i] === '+' || operators[i] == '-' || operators[i] == '*' || operators[i] == '/') {
if (numbers[i + 1] != "undefined") {
result = eval(result + operators[i] + parseFloat(numbers[i+1]));
if (isNaN(result)) {
$("#txt3").html(result.toFixed(2));
} else {
$("#txt3").html(result.toFixed(2));
}
}
}
}
}
}
Paul RajsPosted Dec 7, 2016, 11:53 PM
I hope all of you now know my Requirements . i need to do one more thing thst is how am do this as well as in C#.
i want this Same Calculations in Server Side Too.So anyone having idea please suggest me.
Sorry for my Repeated Post
thanking youPaul.S
Paul RajsPosted Dec 7, 2016, 11:48 PM
Paul RajsPosted Dec 7, 2016, 11:48 PM
Paul RajsPosted Dec 7, 2016, 11:47 PM
Paul RajsPosted Dec 7, 2016, 11:10 PM
Paul RajsPosted Dec 7, 2016, 6:54 AM
Midhun TpPosted Dec 7, 2016, 6:30 AM