javascript - 获取 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()
但不确定何时/如何在这种情况下实现它。
解决方案
只需这样做:
.then(data=>console.log(Math.round(data.main.temp)))
推荐阅读
- r - 如何计算一行在未知数量的列中具有值的次数?
- java - 从 Minio 下载为 zip
- javascript - 发送 HTTP 请求返回 DOMException:发生网络错误
- python - 树莓派零看不到 i2c 设备
- manim - 如何不断更新manim中的一组行
- html - 通过“锚点”从 HTML 文件交叉链接到 XML 中的特定元素
- javascript - 超链接在 Ext 中不可点击。JS html 编辑器
- spring-boot - 单元测试没有问题,但是拦截器会出现问题
- python - 具有两个条件以包含 NaN 的布尔索引
- firebase - 如何解决flutter.gradle和flutter.bat错误?'D:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' 行:1052'