Ruby Vector round() 函数
round() 是Ruby中的一个内置方法,它返回一个新的向量,其条目被四舍五入到给定的精度。
语法 :vec1.round()
参数 :该函数接受一个参数
返回值 : 它返回一个新的向量,其条目被四舍五入到给定的精度。
例子 1 :
# Ruby program for round() method in Vector
# Include matrix
require "matrix"
# Initialize the vector
vec1 = Vector[1.145332, 2.932423, 3.1332445]
# Prints vector rounded to given precision
puts vec1.round(3)
输出:
Vector[1.145, 2.932, 3.133]
例2 :
# Ruby program for round() method in Vector
# Include matrix
require "matrix"
# Initialize the vector
vec1 = Vector[1.145332, 2.932423, 3.1332445]
# Prints vector rounded to given precision
puts vec1.round(0)
输出:
Vector[1, 3, 3]