gpt4 book ai didi

javascript - 计算器的脚本计算范围错误

转载 作者:行者123 更新时间:2023-12-02 13:51:51 25 4
gpt4 key购买 nike

我有一个问题。我有一个简单的计算器脚本,但该脚本开始计算不正确。如果我输入“1000”,它会计算正确(10 美元),如果我输入 50000,它也会计算正确(380 美元)。但如果我输入 10000,它就会计算错误。

范围是:

0 – 1000 = $0.01
10000 – 10000 = $0.009
10000 – 25000 = $0.0084
25000 – 50000 = $0.0076
50000+ = $0.0076

不幸的是,我不是 Javascript 专家,因此我将感谢您的帮助。

function priceCalculation(a){
if(a <= 1000){
return 0.001;
}else if(a >= 1001 && a <= 10000 ){
return 0.009;
}else if(a >= 10001 && a <= 25000 ){
return 0.0084;
}else if(a >= 25001 && a <= 50000 ){
return 0.0076;
}else{
return 0.0076;
}
}

// number format set to en-US e.g (from 1500 to 1,500)
var numFormat = new Intl.NumberFormat("en-US");

$('#likecount').keyup(function(e){
// if a '.' is pressed
if($(this).val().endsWith('.')) {
return;
}

// if input value is empty then assign '0' else the original value
var inputVal = $(this).val() === ''?'0':$(this).val();

inputVal = parseFloat(inputVal.replace(/[$|,]/g, ''));
var price = priceCalculation($(this).val());
var total = (inputVal * price);
total = (Math.round(total * 100) / 100).toFixed(2);
var formatted = numFormat.format(inputVal) // set format to input
$(this).val(formatted); // display the formatted input back
$('#output').text((total)); // display the total price
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input placeholder="How many Instagram likes?" style="height: 50px;width: 360px;color: #222;border-radius: 5px;border: 1px #85c9e3 solid;font-size: 18px;" type="text" id="likecount" />
<p style="font-family: 'Montserrat', sans-serif; padding: 20px 0px; font-weight: bold; color: #222; ">Pricing: <b><span style="color: #004f04;"> $</span><span id="output" style="color: #004f04;"></span></b></p>

最佳答案

该行无效。

var price = priceCalculation($(this).val());

$(this).val() 返回一个字符串。当您有 , 时(数千个),它会调用您的 priceCalculation 函数并返回最后一个 else 语句,即 返回0.0076;

由于您已经将 inputVal 解析为数字,因此您可以像这样使用它:

var price = priceCalculation(inputVal);

最终结果,

function priceCalculation(a) {
if (a <= 1000) {
return 0.001;
} else if (a >= 1001 && a <= 10000) {
return 0.009;
} else if (a >= 10001 && a <= 25000) {
return 0.0084;
} else if (a >= 25001 && a <= 50000) {
return 0.0076;
} else {
return 0.0076;
}
}

// number format set to en-US e.g (from 1500 to 1,500)
var numFormat = new Intl.NumberFormat("en-US");

$('#likecount').keyup(function(e) {
if ($(this).val().endsWith('.')) {
return;
}

var inputVal = $(this).val() === '' ? '0' : $(this).val();

inputVal = parseFloat(inputVal.replace(/[$|,]/g, ''));
var price = priceCalculation(inputVal);
var total = (inputVal * price);
total = (Math.round(total * 100) / 100).toFixed(2);
var formatted = numFormat.format(inputVal);
$(this).val(formatted);
$('#output').text((total));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input placeholder="How many Instagram likes?" style="height: 50px;width: 360px;color: #222;border-radius: 5px;border: 1px #85c9e3 solid;font-size: 18px;" type="text" id="likecount" />
<p style="font-family: 'Montserrat', sans-serif; padding: 20px 0px; font-weight: bold; color: #222; ">Pricing: <b><span style="color: #004f04;"> $</span><span id="output" style="color: #004f04;"></span></b>
</p>

关于javascript - 计算器的脚本计算范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40968962/

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