题解 | #取近似值#
取近似值
http://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a
利用取模来判断后面0.5来去最近值
const readline = require('readline')
const r = readline.createInterface({
input: process.stdin,
ouput: process.stdout
})
r.on('line', (line)=>{
const value = (parseFloat(line) * 10) % 10
if (value >= 5) {
console.log(Math.ceil(line))
} else {
console.log(Math.floor(line))
}
})
还有一种最简单的就是+0.5向下取值
const readline = require('readline')
const r = readline.createInterface({
input: process.stdin,
ouput: process.stdout
})
r.on('line', (line)=>{
console.log(Math.floor(line*1 + 0.5))
})