如何使用Javascript将整数向上或向下舍入到最接近的10 [英] How to round an integer up or down to the nearest 10 using Javascript

查看:80
本文介绍了如何使用Javascript将整数向上或向下舍入到最接近的10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Javascript,我想将用户传递的数字四舍五入到最近的10.例如,如果传递7,我应该返回10,如果33传递,我应该返回30。

Using Javascript, I would like to round a number passed by a user to the nearest 10. For example, if 7 is passed I should return 10, if 33 is passed I should return 30.

推荐答案

将数字除以10,将结果四舍五入再乘以10:

Divide the number by 10, round the result and multiply it with 10 again:

var number = 33;
alert(Math.round(number / 10) * 10);

这篇关于如何使用Javascript将整数向上或向下舍入到最接近的10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆