gpt4 book ai didi

JavaScript 算术返回意外值

转载 作者:行者123 更新时间:2023-11-30 12:43:45 26 4
gpt4 key购买 nike

这是一个简单的 JS 片段,

$("#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);
}
});
});

我通过 JSON AJAX 请求获得的值是,

{"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}

如果我单独打印值,它会显示,

var mrp_value = 35;
var quantity = 4;

赋值是, 变量 pts_value = 5; 变量增值税 = 5;

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

所以,我希望这会按如下方式执行,

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

但它返回 1400 作为值。我为什么以及在哪里犯了错误?

最佳答案

在 JavaScript 中,“5”+ 0 =“50”。确保您的变量是数字而不是字符串。

使用 Number()确保功能。

关于JavaScript 算术返回意外值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23397824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com