冉冉说
不是最短但工作function roundNumberWith05 (num){ const diff = num - Math.floor(num); if (diff < 0.25 || diff > 0.75) { return Math.round(num * 2) / 2; } else { return num - diff + 0.5; }}console.log('2.1 --', roundNumberWith05(2.1));console.log('2.4 --', roundNumberWith05(2.4));console.log('1.9 --', roundNumberWith05(1.9)); console.log('1.75 --', roundNumberWith05(1.75)); console.log('1.74 --', roundNumberWith05(1.74)); console.log('1.76 --', roundNumberWith05(1.76)); console.log('2.688 --', roundNumberWith05(2.688)); console.log('2.2588 --', roundNumberWith05(2.2488));