如何在javascript中找到最近的最高千位

par*_*nfo 2 javascript jquery

我需要从整数中找到最接近的最高 1000

例如

let Num = 110;  //result will be 1000 
let num2 = 1280 // result will be 2000 
Run Code Online (Sandbox Code Playgroud)

我已经尝试过下面的示例,但它也给出了最低值

var round = Math.round(Num) // I am getting 100 only 
Run Code Online (Sandbox Code Playgroud)

vol*_*ron 5

除以要舍入到的十位,然后乘以该数字。使用Math.ceil所以它总是四舍五入:

let num1 = 110
let num2 = 1280
let num3 = -110

console.log( nearestThousand(num1) )  // 1000
console.log( nearestThousand(num2) )  // 2000
console.log( nearestThousand(num3) )  // 0 <-- determine expected behavior

function nearestThousand(n){
  return Math.ceil(n/1000)*1000
}
Run Code Online (Sandbox Code Playgroud)