题解 | 取近似值

取近似值

https://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    // Write your code here
    while(line = await readline()){
        // 最简单的。
        // console.log(Math.round(line));

        let theString = ``
        let thePix = ``
        // 遍历,得到小数点前的字符串。以及小数点后一位。
        for(let index = 0;index<line.length;index++){
            if(line[index] !== '.'){
                theString = theString + line[index]
            }
            if(line[index] === '.'){
                thePix = line[index+1] || '0'
                break
            }
        }
        // 得到小数点前的整数。默认不进位。
        let theNumber = Number(theString)
        // 如果小数点后一位大于5,则进一位。
        if(Number(thePix)>=5){
            theNumber = theNumber + 1
        }
        console.log(theNumber)

    }
}()

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务