红宝石 |矩阵 round()函数
round()是 Ruby 中的一个内置方法,它返回四舍五入到小数点后给定位数的矩阵的所有值。如果未传递任何参数,则假定 0 为默认值。
Syntax: mat1.round(num)
Parameters: The function takes a non-mandatory parameter num to which the values in the matrix are rounded to. In case num is not passed, it is assumed to be zero.
Return Value: It returns the matrix with all values rounded to num digits after decimal point.
示例 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]]