目录

round()

描述 (Description)

round()方法接受一个数字,并返回舍入到最接近整数的数字的值

语法 (Syntax)

下面给出了JavaScript的round()方法的语法。 我们可以在CoffeeScript代码中使用相同的方法。

Math.round ( x )

例子 (Example)

以下示例演示了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 

打开command prompt并编译.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);

现在,再次打开command prompt ,然后运行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
↑回到顶部↑
WIKI教程 @2018