📜  红宝石 |矩阵 round()函数

📅  最后修改于: 2022-05-13 01:54:53.036000             🧑  作者: Mango

红宝石 |矩阵 round()函数

round()是 Ruby 中的一个内置方法,它返回四舍五入到小数点后给定位数的矩阵的所有值。如果未传递任何参数,则假定 0 为默认值。

示例 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]]