Coffeescript-math-round

提供:Dev Guides
移動先:案内検索

CoffeeScript Math-round()

説明

  • round()*メソッドは数値を受け入れ、数値を最も近い整数に丸めた値を返します

構文

以下に、JavaScriptの* round()*メソッドの構文を示します。 CoffeeScriptコードで同じメソッドを使用できます。

Math.round ( x )

次の例は、CoffeeScriptでの* round()メソッドの使用方法を示しています。 このコードを *math_round.coffee という名前のファイルに保存します。

value = Math.round 0.5
console.log "The nearest integer to 0.5 is : " + value

value = Math.round 20.7
console.log "The nearest integer to 20.7 is : " + value

value = Math.round -20.3
console.log "The nearest integer to -20.3 is : " + value
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c math_round.coffee

コンパイル時に、次のJavaScriptが提供されます。

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

  value = Math.round(0.5);

  console.log("The nearest integer to 0.5 is : " + value);

  value = Math.round(20.7);

  console.log("The nearest integer to 20.7 is : " + value);

  value = Math.round(-20.3);

  console.log("The nearest integer to -20.3 is : " + value);

}).call(this);

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee math_round.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。

The nearest integer to 0.5 is : 1
The nearest integer to 20.7 is : 21
The nearest integer to -20.3 is : -20