JavaScript Math.ceil()函数用法详细介绍

2021年3月14日14:38:06 发表评论 658 次浏览

Math.ceil()JavaScript中的函数用于将作为参数传递的数字四舍五入为向上方向四舍五入, 即朝着更大的价值迈进。

语法

Math.ceil(value)

参数:

此函数接受单个参数, 即要四舍五入到其数字

向上舍入法中最接近的整数。

返回值:

将作为参数传递的数字四舍五入后的结果。

例子:

Input  : Math.ceil(4.23)
Output : 5

Input  : Math.ceil(0.8)
Output : 1

错误和异常:

  • 一种非数字字符串作为参数传递的返回NaN
  • An大于1的整数数组作为参数传递的返回NaN
  • An空变量传递作为参数返回NaN
  • An空字符串作为参数传递的返回NaN
  • An空数组作为参数传递的返回NaN

以下示例说明了JavaScript中的Math.ceil()函数:

范例:1

<!-- NEGATIVE NUMBER EXAMPLE -->
< script type = "text/javascript" >
     document.write(Math.ceil(-2) + "< br >"); 
     document.write(Math.ceil(-2.56));          
</ script >

输出如下:

-2
-2

示例:2

<!-- POSITIVE NUMBER EXAMPLE -->
< script type = "text/javascript" >
     document.write(Math.ceil(2) + "< br >"); 
     document.write(Math.ceil(2.56));          
</ script >

输出如下:

2
3

范例2:

<!-- STRING EXAMPLE -->
< script type = "text/javascript" >
     document.write(Math.ceil("lsbin"));          
</ script >

输出如下:

NaN

示例:3

<!-- ADDITION INSIDE FUNCTION EXAMPLE -->
< script type = "text/javascript" >
     document.write(Math.ceil(7+9));           
</ script >

输出如下:

16

支持的浏览器:支持的浏览器JavaScript Math.ceil()函数下面列出:

  • 谷歌浏览器
  • IE浏览器
  • 火狐浏览器
  • 歌剧
  • 苹果浏览器

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: