我有一些 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/