如何将小数舍入为整数

时间:2019-03-27 14:54:28

标签: javascript

我正在尝试获取一个整数(整数),而输出始终为十进制。我该如何改变?

我尝试搜索堆栈溢出,但是找不到类似的问题

// Javscript exercise taken from internet for absolute beginners

var people = prompt("how many people are there? ");
var pizzas = prompt("How many pizzas are there? ");
const slices = 8;

var perPerson = ((pizzas * slices) / people);

var leftovers = perPerson % 2;

if (leftovers !== 0) { 
    Math.round(leftovers);
}

prompt(`There are a total of ${people} people with ${pizzas} pizzas`);
prompt(`Each person gets ${perPerson} slices of pizza`);
prompt(`There are ${leftovers} leftover slices of pizza`);

我期待整数,但是我只得到小数

1 个答案:

答案 0 :(得分:0)

您必须在leftovers中像这样重置leftovers=Math.round(leftovers);

var people = prompt("how many people are there? ");
    var pizzas = prompt("How many pizzas are there? ");
    const slices = 8;

    var perPerson = ((pizzas * slices) / people);

    var leftovers = perPerson % 2;

    if (leftovers !== 0) {
        leftovers=Math.round(leftovers);
    }

    prompt(`There are a total of ${people} people with ${pizzas} pizzas`);
    prompt(`Each person gets ${perPerson} slices of pizza`);
    prompt(`There are ${leftovers} leftover slices of pizza`);