Javascript 将范围四舍五入到步长值

Javascript 将范围四舍五入到步长值,javascript,typescript,Javascript,Typescript,我有一个像这样的数字数组: 常量数据集=[0.5,2,1,93,67.5,1,7,34]; 所以最小值是0.5,最大值是93。我想将数据集的极值四舍五入为步长值 例如: 如果步骤=5,结果应为[0,95] 如果步骤=10,结果应为[0,100] 新的最小值应该始终=数据集中的实际最大值,并且它们都应该是步长的倍数 注意:如果它也适用于负值,我会很高兴 我创建了roundToNearest函数,但不足以解决我的问题: 函数computeExtremisRoundeddataset:编号[],步骤:

我有一个像这样的数字数组:

常量数据集=[0.5,2,1,93,67.5,1,7,34]; 所以最小值是0.5,最大值是93。我想将数据集的极值四舍五入为步长值

例如:

如果步骤=5,结果应为[0,95] 如果步骤=10,结果应为[0,100] 新的最小值应该始终=数据集中的实际最大值,并且它们都应该是步长的倍数

注意:如果它也适用于负值,我会很高兴

我创建了roundToNearest函数,但不足以解决我的问题:

函数computeExtremisRoundeddataset:编号[],步骤:编号:[编号,编号]{ const[minValue,maxValue]=getMinAndMaxdataset//假设它存在 const roundedMinValue=roundToNearestminValue,步长 const roundedMaxValue=roundToNearestmaxValue,步长 返回[roundedMaxValue,roundedMaxValue] } 函数roundToNearestvalue:number,步骤:number:number{ 返回Math.roundvalue/step*step; }
根据计算最大值还是最小值,您必须选择天花板或地板:

function computeExtremisRounded(dataset: number[], step: number): [number, number] {
   const [minValue, maxValue] = getMinAndMax(dataset) // suppose it exists
   const roundedMinValue = Math.floor(minValue / step) * step
   const roundedMaxValue = Math.ceil(maxValue / step) * step
   return [roundedMinValue, roundedMaxValue]
}

根据计算最大值还是最小值,您必须选择天花板或地板:

function computeExtremisRounded(dataset: number[], step: number): [number, number] {
   const [minValue, maxValue] = getMinAndMax(dataset) // suppose it exists
   const roundedMinValue = Math.floor(minValue / step) * step
   const roundedMaxValue = Math.ceil(maxValue / step) * step
   return [roundedMinValue, roundedMaxValue]
}