Ruby | Matrix round() function
Last Updated :
07 Jan, 2020
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:
require "matrix"
mat1 = Matrix[[ 1 . 878787 , 21 . 8449 ], [ 31 . 7382 , 18 . 7382 ]]
puts mat1.round( 2 )
|
Output:
Matrix[[1.88, 21.84], [31.74, 18.74]]
Example 2:
require "matrix"
mat1 = Matrix[[ 6 . 4334 , 432 . 432 ], [ 54 . 342 , 323 . 213 ]]
puts mat1.round()
|
Output:
Matrix[[6, 432], [54, 323]]
Share your thoughts in the comments
Please Login to comment...