首页 > 解决方案 > 获取 api,如何对您收到的数据进行四舍五入

问题描述

let input=document.querySelector('#city');
let submit= document.querySelector('#submit');
let output=document.querySelector('#output');


submit.addEventListener('click', ()=>{
    let url=`https://api.openweathermap.org/data/2.5/weather?q=${input.value}&APPID=758bab291826491e79f93979de2ba255&units=imperial`;
    fetch(url)
            .then(response=> response.json())
            .then(data=>console.log(data.main.temp))  
})

收到温度,但它是小数形式,我希望它是一个整数,我知道Math.round()但不确定何时/如何在这种情况下实现它。

标签: javascriptjqueryhtml

解决方案


只需这样做:

.then(data=>console.log(Math.round(data.main.temp)))

推荐阅读