Javascript jQuery向上取整到最接近的整数

Javascript jQuery向上取整到最接近的整数,javascript,jquery,Javascript,Jquery,我正在尝试将.qty字段四舍五入到最接近的整数。我真的不确定在下面的代码段中放在哪里?我想我应该用Math.ceil() 这可以通过使用基本javascript实现: 或使用: Math.floor(number here); <- this rounds it DOWN so 4.6 becomes 4 Math.round(number here); <- this rounds it UP so 4.6 becomes 5 我可以在这个函数中放置一个变量吗?谢谢。如果问题是“

我正在尝试将.qty字段四舍五入到最接近的整数。我真的不确定在下面的代码段中放在哪里?我想我应该用Math.ceil()


这可以通过使用基本javascript实现: 或使用:

Math.floor(number here); <- this rounds it DOWN so 4.6 becomes 4
Math.round(number here); <- this rounds it UP so 4.6 becomes 5

我可以在这个函数中放置一个变量吗?谢谢。如果问题是“汇总”,您可能希望使用
Math.ceil()
而不是
Math.floor()
@ru pearls,只要这个变量是,或者是一个数字,那么是的。我用你的例子编辑了这篇文章,我相信计算需要四舍五入,所以我使用了这个变量,然后请选择它作为答案:)
Math.floor(number here); <- this rounds it DOWN so 4.6 becomes 4
Math.round(number here); <- this rounds it UP so 4.6 becomes 5
function (){
var sm = parseFloat($(this).val());
var tsm = parseFloat($('.tsm', $(this).parent().parent()).val());
var calc = (sm*tsm); // total tiles needed

if($('.addwaste', $(this).parent().parent()).is(':checked')){ 
    var onepercent = calc/100;
    var sevenpercent = Math.ceil(onepercent*7);
    calc+=sevenpercent;
}

calc = Math.round(calc);

$('.qty', $(this).parent().parent()).val(calc);
$('div.product form.cart .qty').val( calc );

var rawprice = parseFloat($('.rawprice', $(this).parent().parent()).val());
var total = (rawprice*calc).toFixed(2);

$('.total', $(this).parent().parent()).html(total);
$('div.product .price .amount').html( '£' + total );    
}