“round” 是一个在编程语言中常用的函数,用于四舍五入一个数值。它的作用是将一个数近似到最近的整数,但具体怎么处理取决于小数部分的大小。
✅ 一、在编程语言中(如 Python、JavaScript、Java 等)的 round 函数
1. Python 中的 round() 函数
print(round(2.7)) # 输出 3
print(round(2.3)) # 输出 2
print(round(2.999)) # 输出 3
print(round(-2.5)) # 输出 -2(Python 的 round 函数是“银行家舍入法”)
2. JavaScript 中的 Math.round() 函数
console.log(Math.round(2.7)); // 输出 3
console.log(Math.round(2.3)); // 输出 2
console.log(Math.round(2.999)); // 输出 3
console.log(Math.round(-2.5)); // 输出 -2
3. Java 中的 Math.round() 函数
double d = 2.7;
System.out.println(Math.round(d)); // 输出 3