1

Here is a simple JS snippet,

$("#product_code").live('change',function(){
    $.ajax({
        type:'POST',
        url:baseurl+'ajax/getproduct/'+$(this).val(),
        success:function(productdetails){
            var bill_type = $("#bill_type").val();
            var mrp_value = productdetails['mrp_value'];
            var quantity = productdetails['quantity'];

            $("#product_name").val(productdetails['product_name']);
            $("#packing").val(productdetails['packing']);
            $("#mrp_value").val(productdetails['mrp_value']);
            $("#batch_number").val(productdetails['batch_number']);
            $("#manufacturing_date").val(productdetails['manufacturing_date']);
            $("#expiry_date").val(productdetails['expiry_date']);
            $("#quantity").val(productdetails['quantity']);

            switch(bill_type)
            {
                case 'stockies':
                        var pts_value = 5; //In percent
                        var vat = 5; //In percent
                        var total_value = (mrp_value+(mrp_value*vat/100)-(mrp_value*pts_value/100))*quantity;                               
                    break;
                case 'pharmacy':
                        var pts_value = 3; //In percent
                        var vat = 5; //In percent
                        var total_value = (mrp_value+(mrp_value*vat/100)-(mrp_value*pts_value/100))*quantity;                               
                    break;
                case 'replacement':
                        var pts_value = 0; //In percent
                        var vat = 0; //In percent
                        var total_value = 0;
                    break;
            }

            $("#pts_value").val(pts_value);
            $("#vat").val(vat);

            $("#total_value").val(total_value);
        }
    }); 
});

And the value i'm getting through JSON AJAX request is,

{"stock_id":"1","product_code":"AG123456","product_name":"Test Product","packing_area":"10x10","bottle_size":"170ml","product_type":"bottle","chemical_contents":"HCL","batch_number":"12","manufacturing_date":"2012-03-12","expiry_date":"2014-03-12","quantity":"4","packing":"Hard","purchase_value":"34","sales_value":"36","mrp_value":"35","status":"0","created_date":"2014-04-27 14:05:17","modified_date":null,"deleted_date":null}

If i print the values separately, it is showing,

var mrp_value = 35;
var quantity = 4;

and the assigned values are, var pts_value = 5; var vat = 5;

var total_value = (mrp_value+(mrp_value*vat/100)-(mrp_value*pts_value/100))*quantity;

So, i'm expecting this will execute as follows,

  • (35 + (35*5/100) - (35*5/100) ) * 4
  • (35 + (1.75) - (1.75)) * 4
  • (35 + (0)) * 4
  • 140

But it is returning 1400 as value. Why and where i'm doing the mistake?

2
  • 3
    I didn't go through your code, but my money's on one or more values being treated as a string instead of as a number. Commented Apr 30, 2014 at 20:56
  • 1
    All the numbers in the JSON are strings. You could try fixing the server script to return numbers instead. Commented Apr 30, 2014 at 21:02

2 Answers 2

3

In JavaScript, "5" + 0 = "50". Make sure your variables are numbers and not strings.

Use the Number() function to be sure.

Sign up to request clarification or add additional context in comments.

Comments

0

See this input:

var pts_value = 5;
var vat = 5;
var mrp_value = 35;
var quantity = 4;
console.log((mrp_value+(mrp_value*vat/100)-(mrp_value*pts_value/100))*quantity);

var mrp_value = '35';
var quantity = '4';
console.log((mrp_value+(mrp_value*vat/100)-(mrp_value*pts_value/100))*quantity);

This gives this output:

c:\it\nodejs>node.exe 01_hello.js
140
1400

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.