javascript - 检查数字是否是 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 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/

相关文章:

javascript - 为什么我的回调没有被调用?

javascript - 使用 KnockoutJS 搜索过滤器

javascript - 在客户端从 base64 图像字符串(使用 javascript 或 jquery)中提取 exif

javascript - 如何执行按钮提交弹出一个窗口,其中包含指向其他站点的图像?

javascript - 使用 JCrop 重新缩放大图像

javascript - jquery 表单选择器单选按钮和复选框

javascript - 使用 adobe air 和 javascript 进行 Onchange 事件

javascript - 处理订单队列 (Redis) 的最佳选择是什么?

javascript - 数组按下按钮点击jquery

javascript - 在 Meteor 中使用 custombox.js 打开模式弹出窗口