使用 JavaScript 四舍五入到最接近的 30

Round to nearest 30 using JavaScript

我有一个按 90 天销售的产品系列,最低面额为每月 1 个。产品能用多久取决于用户的消费情况。

一些示例:

如果产品持续使用 28 天,那么我需要 JavaScript 将售出的单位数量更改为每月 2 件。

如果产品持续使用 35 天,则将售出的单位数保留为每月 1 件。

如果产品持续使用 61 天,则将每月售出的单位数保留为 1。

如果产品持续 10 天,则更改为每月 3 天。

var unitsPerMonth = 0;
var numberOfDays = productsAmount / userConsumption;
// example 1500 / 52 per day = product will last 28 days, so round unitsPerMonth to 2

我认为您正在寻找 Math.ceil 将数字四舍五入为整数。

var numberOfDays = productsAmount / userConsumption;
var salesPer30Days = 30 / numberOfDays;
var unitsPerMonth = Math.ceil(salesPer30Days);