将一个浮点数上升到javascript中的下一个整数 [英] Round a float up to the next integer in javascript

查看:200
本文介绍了将一个浮点数上升到javascript中的下一个整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将浮点数整数到最接近的整数,即使点后的数小于0.5。例如,




  • 4.3应该是5(不是4)
    应该是5


如何在JavaScript中执行此操作?

解决方案

一个href =https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/ceil> Math.ceil [MDN] 函数

  var n = 4.3; 
alert(Math.ceil(n)); //警报5


I need to round floating point numbers up to the nearest integer, even if the number after the point is less than 0.5.

For example,

  • 4.3 should be 5 (not 4)
  • 4.8 should be 5

How can I do this in JavaScript?

解决方案

Use the Math.ceil[MDN] function

var n = 4.3;
alert(Math.ceil(n)); //alerts 5

这篇关于将一个浮点数上升到javascript中的下一个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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