簡體   English   中英

四舍五入至200,如果為200,則為-1

[英]round nearest 200 but -1 if 200

我有一排歌,但從0-200開始顯示。進入下一頁時,它從201-400開始。下一頁從401到600等。

我有自動滾動功能,但是我需要它跳到當前歌曲正在播放的頁面,然后滾動到該歌曲? 因此,如果歌曲為:250,則我的起始點不應為201,結束顯示應為400。但是,如果歌曲為200,則我的起始則不應為0,結束顯示為200。我需要提出一種解決方法到最接近的200是其250 260等,但是如果其200,400,600等則為-1,所以如果其200,-1為199,它將四舍五入為0。

也許有更好的解決方案? 無論如何,下面是我的四舍五入代碼--很好!

碼:

scrolltopage = destination + 1; //number could be 20,201,288 etc 
startpageQueueScroll = parseInt(Math.floor(scrolltopage / 200) * 200 + 1); //Start displaying contents at
endpageQueueScroll = parseInt(Math.floor(scrolltopage / 200) * 200 + 201); //Stop displaying at 
console.log("Start: " + startpageQueueScroll + " and end page: " + endpageQueueScroll); //Display Output

如何使用模運算符? 樣例代碼:

    function getNextPages(original){
    const nextDestination = original + 1;
    const nextModulus = nextDestination % 200;
    if(nextModulus == 0){
        return -1;
    }
    const nextMinPage = nextDestination - nextModulus + 1;
    const nextMaxPage = nextMinPage + 200;
    console.log(nextMinPage);
    console.log(nextMaxPage);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM