You must Sign In to post a response.
  • Category: JQuery

    How to Calculate also Decimals with in this Code in Jquery

    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
  • #768483
    Hi,

    I am not sure why it is so difficult to do it.
    While adding decimals we need to parse the values.

    So what actually we are doing here like, You have combination of INT and DECIMALS.
    You can convert DECIMAL to INT but data loss is there. But you can convert INT to DECIMALS without any data loss.

    So convert all the INT which is coming as input so obviously you will get the DECIMAL output.

    jQuery(".class #method").change(function(){
    var costing = jQuery(this).val();
    jQuery("#textbox1").val(cost); //suppose returns 20.00
    var totals = parseFloat(cost) + parseFloat(total); //total returns 71.96
    });

    Thanks,
    Mani

  • #768499
    I got Exact output with this Query Friends.

    <script src="Tracking/js/jquery.js"></script>
    <script src="Tracking/js/jquery.js"></script>
    <script>
    function txtpercentage() {
    var SumofTotal;
    var Quantity = $('#txt2').val();
    var numericReg = /^[0-9.*+-/]+$/;

    var str = Quantity;
    var numbers = str.replace().split(/[-+*\/]/g);
    var operators = str.replace(/\./g, '').split(/\d*/g);
    operators.shift();
    var result = +numbers[0];
    // alert(result);
    $("#txt3").html(result.toFixed(2));
    for (var i = 0; i < operators.length; i++) {
    result = eval(result + operators[i] + numbers[i + 1]);
    if (isNaN(result)) {
    $("#txt3").html(result.toFixed(2));
    }
    else {

    $("#txt3").html(result.toFixed(2));
    }
    }
    }
    </script>

    Html

    <table>
    <tr>
    <td style="padding-LEFT: 240PX;">
    <asp:Label ID="Label50" runat="server" Text="FREIGHT" Font-Bold="true"></asp:Label>
    <span style="float: right; color: blue;">: </span>
    </td>
    <td>
    <asp:TextBox ID="txt2" runat="server" onkeyup="txtpercentage()" Font-Bold="true" ForeColor="Black" AutoComplete="off"></asp:TextBox>
          
    <asp:Label ID="txt3" runat="server" ForeColor="Red" Text="0.00" Font-Bold="true" Font-Size="17px"></asp:Label>
    <br />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator18" runat="server" ControlToValidate="txt2"
    ValidationGroup="CsbEdit" ErrorMessage="This field isRequired !" ForeColor="Red" SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator102" runat="server" ValidationExpression="^[0-9.*+-/]+$"
    ValidationGroup="CsbEdit" ErrorMessage="Invalid value !.. - Integer,Decimal + , - , * , / only Allowed. !.." ControlToValidate="txt2" ForeColor="Red" Display="Dynamic" />
    </td>
    </tr>
    </table>
    thanking You
    Paul.S

  • #768500
    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 you
    Paul.S


  • Sign In to post your comments