CoffeeScript 数学 - 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

