在Javascript/jQuery中检查数字是否为整数
我有一些JS:在Javascript/jQuery中检查数字是否为整数,javascript,jquery,Javascript,Jquery,我有一些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 th
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();
}
};
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();
var val = amount + '' + $currencyTo.find(':selected').text();
if (val === parseInt(val)){// check if val is an Int
$('#you-get').text(val.toFixed(2));//add .00 if amount + $currencyTo.find is an Int
}else{
$('#you-get').text(val);
}
}else{
$('.you-get').hide();
}
};
试试这个:
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();
var val = amount + '' + $currencyTo.find(':selected').text();
if (val === parseInt(val)){// check if val is an Int
$('#you-get').text(val.toFixed(2));//add .00 if amount + $currencyTo.find is an Int
}else{
$('#you-get').text(val);
}
}else{
$('.you-get').hide();
}
};
你能用余数检查这个数字是不是整数吗?例如
var val = parseFloat(amount) + parseFloat($currencyTo.find(':selected').text());
if ( val % 1 == 0 ) { /* whole number */ } else {/* not whole number */ }
你也可以检查一下!伊斯南瓦尔
这基本上是一个类似问题的答案,请参见您能用余数检查数字是否为整数吗?例如
var val = parseFloat(amount) + parseFloat($currencyTo.find(':selected').text());
if ( val % 1 == 0 ) { /* whole number */ } else {/* not whole number */ }
你也可以检查一下!伊斯南瓦尔
这基本上是一个类似问题的答案,请参见“有方法”
没有办法
金额=数字金额;比率=数字;这会有帮助吗?也许是数量;我不知道你所说的amount+$currencyTo是什么意思。因为你在+''+amount=Numberamount之间加了一个空格,所以我认为它应该是一个字符串而不是一个数字;比率=数字;这会有帮助吗?也许是数量;我不知道你所说的amount+$currencyTo是什么意思。因为你在+''之间加了空格,所以我认为这应该是一个字符串而不是一个数字。嗨,谢谢,如果变量val中没有小数点,它似乎不会加.00在本例中,toFixed在整数上加.00。您好,谢谢,如果变量val?中没有小数点,它似乎不会加.00:在本例中,toFixed在整数上加.00。