gpt4 book ai didi

javascript - 检查数字是否是 Javascript/jQuery 中的整数

转载 作者:行者123 更新时间:2023-11-30 08:03:40 25 4
gpt4 key购买 nike

我有一些 JS:

var updateAmount = function (amount, rate) {
if (rate > 0 && amount.length > 0 && !isNaN(amount) && amount > 0) {
amount = Math.round(rate * amount * 100) / 100; // multiply rate with amount and then round to 2 decimals
$('.you-get').show();
$('#you-get').text(amount + ' ' + $currencyTo.find(':selected').text());
} else {
$('.you-get').hide();
}
};

我需要的是一个子句,它检查从amount + $currencyTo.find 生成的值是否为整数,如果是则添加.00 到最后。

最佳答案

if(amount === parseInt(amount)) //returns `true` if it's a integer


var updateAmount = function (amount, rate) {
if (rate > 0 && amount.length > 0 && !isNaN(amount) && amount > 0 && amount === parseInt(amount)) { //Edited this line
if(amount === parseInt(amount)) amount += '.00'; //Added this line
amount = Math.round(rate * amount * 100) / 100; // multiply rate with amount and then round to 2 decimals
$('.you-get').show();
$('#you-get').text(amount + ' ' + $currencyTo.find(':selected').text());
} else {
$('.you-get').hide();
}
};

关于javascript - 检查数字是否是 Javascript/jQuery 中的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22010661/

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