Open In App

Ruby | Matrix round() function

The round() is an inbuilt method in Ruby returns all the values of the matrix rounded to the given number of digits after decimal point. In case no parameter is passed, then 0 is assumed to be the default value.

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.



Example 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)

Output:

Matrix[[1.88, 21.84], [31.74, 18.74]]

Example 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()

Output:

Matrix[[6, 432], [54, 323]]
Article Tags :