如何在JavaScript中舍入到整数? [英] How can I round to whole numbers in JavaScript?

查看:116
本文介绍了如何在JavaScript中舍入到整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来计算某个百分比:

I have the following code to calculate a certain percentage:

var x = 6.5;
var total;

total = x/15*100;

// Result  43.3333333333

我想要的结果是确切的数字 43 如果总数 43.5 ,则应舍入为 44

What I want to have as a result is the exact number 43 and if the total is 43.5 it should be rounded to 44

有没有办法在JavaScript中执行此操作?

Is there way to do this in JavaScript?

推荐答案

使用 Math.round() 函数将结果四舍五入到最接近的整数。

Use the Math.round() function to round the result to the nearest integer.

这篇关于如何在JavaScript中舍入到整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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