Ruby 矩阵 round() 函数
round() 是Ruby中的一个内置方法,用于返回矩阵的所有数值,并将其四舍五入到小数点后的指定数字。如果没有传递参数,则假定0为默认值。
语法 :mat1.round(num)
参数 :该函数需要一个非强制性的参数num,矩阵中的数值将被四舍五入。如果没有传递num,则假定其为零。
返回值 : 返回矩阵中所有数值被四舍五入到小数点后的num数字。
例子 1 :
# Ruby program for round() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[1.878787, 21.8449], [31.7382, 18.7382]]
# Prints all values of matrix
# rounded by 2
puts mat1.round(2)
输出:
Matrix[[1.88, 21.84], [31.74, 18.74]]
例2 :
# Ruby program for round() method in Matrix
# Include matrix
require "matrix"
# Initialize a matrix
mat1 = Matrix[[6.4334, 432.432], [54.342, 323.213]]
# Prints all values of matrix
# rounded by 0 which is default
puts mat1.round()
输出:
Matrix[[6, 432], [54, 323]]