CoffeeScript 數學 - ceil()



描述

ceil() 方法接受一個數字並返回其向上取整值。

語法

以下是 JavaScript 的 ceil() 方法的語法。 我們可以在 CoffeeScript 程式碼中使用相同的方法。

Math.ceil( x )

示例

以下示例演示瞭如何在 CoffeeScript 中使用 ceil() 方法。 將此程式碼儲存在名為 math_ceil.coffee 的檔案中。

value = Math.ceil 90.15
console.log "The ceil value of 90.15 is : " + value 
         
value = Math.ceil 15.90
console.log "The ceil value of 15.90 is : " + value 
         
value = Math.ceil -90.15
console.log "The ceil value of -90.15 is : " + value

value = Math.ceil -15.90 
console.log "The ceil value of -15.90 is : " + value

開啟 命令提示符 並如下所示編譯 .coffee 檔案。

c:\> coffee -c math_ceil.coffee

在編譯時,它會給你以下 JavaScript。

// Generated by CoffeeScript 1.10.0
(function() {
  var value;

  value = Math.ceil(90.15);

  console.log("The ceil value of 90.15 is : " + value);

  value = Math.ceil(15.90);

  console.log("The ceil value of 15.90 is : " + value);

  value = Math.ceil(-90.15);

  console.log("The ceil value of -90.15 is : " + value);

  value = Math.ceil(-15.90);

  console.log("The ceil value of -15.90 is : " + value);

}).call(this);

現在,再次開啟 命令提示符,並如下執行 CoffeeScript 檔案。

c:\> coffee math_ceil.coffee

在執行時,CoffeeScript 檔案輸出以下結果。

The ceil value of 90.15 is : 91
The ceil value of 15.90 is : 16
The ceil value of -90.15 is : -90
The ceil value of -15.90 is : -15
coffeescript_math.htm
廣告