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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Matrix[[6, 432], [54, 323]]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.