round()是Ruby中的一種內置方法,它返回四舍五入為小數點後給定位數的矩陣的所有值。如果未傳遞任何參數,則假定0為默認值。
用法: mat1.round(num)
參數:該函數采用非強製性參數num,矩陣中的值四舍五入為該值。如果未傳遞num,則假定為零。
返回值:返回矩陣,所有值四舍五入到小數點後的數字位數。
例子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]]
相關用法
- Ruby Rational round()用法及代碼示例
- Ruby BigDecimal round()用法及代碼示例
- Ruby Numeric round()用法及代碼示例
- Ruby Time round用法及代碼示例
- Ruby Vector round()用法及代碼示例
- Ruby Matrix I()用法及代碼示例
- Ruby Matrix lup()用法及代碼示例
- Ruby Matrix inv()用法及代碼示例
- Ruby Matrix row()用法及代碼示例
- Ruby Matrix tr()用法及代碼示例
- Ruby Matrix det()用法及代碼示例
- Ruby Matrix t()用法及代碼示例
- Ruby Matrix zero()用法及代碼示例
- Ruby Matrix zero?()用法及代碼示例
- Ruby Matrix eql?用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby | Matrix round() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。