将变量四舍五入为最接近的整数

Rounding a variable to the nearest integer

我有一些代码可以将变量四舍五入为最接近的整数 - 但我似乎无法让它将变量四舍五入为最接近的整数。这是我的代码:

var score = 600;
var coinBase = 400;
var coinInt = score / coinBase;
var coin = round(VARIABLEcoinInt');

换句话说,我试图获取变量 coinInt,将其四舍五入到最接近的整数,并将其输出到变量 coin 中。

没有round函数。请改用 Math.round 函数。

var score = 600;
var coinBase = 400;
var coinInt = score / coinBase;
var coin = Math.round(coinInt);

console.log(coin);

您可以使用 3 个基于 Math 的函数之一。

Math.round - 四舍五入到最接近的整数:

Math.round(1.4) // 1
Math.round(1.5) // 2

Math.floor - 向下舍入

Math.floor(1.2) // 1
Math.floor(1.9) // 1

Math.ceil - 四舍五入

Math.ceil(1.2) // 2
Math.ceil(1.9) // 2