round()是Ruby中的內置方法,它返回一個新的向量,其向量四舍五入到給定的精度
用法: vec1.round()
參數:該函數接受單個參數
返回值:返回一個新的向量,該向量的舍入為給定的精度
例子1:
# Ruby program for round() method in Vector
# Include matrix
require "matrix"
# Initialize the vector
vec1 = Vector[1.145332, 2.932423, 3.1332445]
# Prints vector rounded to given precision
puts vec1.round(3)
輸出:
Vector[1.145, 2.932, 3.133]
例子2:
# Ruby program for round() method in Vector
# Include matrix
require "matrix"
# Initialize the vector
vec1 = Vector[1.145332, 2.932423, 3.1332445]
# Prints vector rounded to given precision
puts vec1.round(0)
輸出:
Vector[1, 3, 3]
相關用法
- Ruby Matrix round()用法及代碼示例
- Ruby Rational round()用法及代碼示例
- Ruby BigDecimal round()用法及代碼示例
- Ruby Time round用法及代碼示例
- Ruby Numeric round()用法及代碼示例
- Ruby Vector eql?()用法及代碼示例
- Ruby Vector zero()用法及代碼示例
- Ruby Vector zero?()用法及代碼示例
- Ruby Vector r()用法及代碼示例
- Ruby Vector size()用法及代碼示例
- Ruby Vector angle_with()用法及代碼示例
- Ruby Vector inner_product()用法及代碼示例
- Ruby Vector cross_product()用法及代碼示例
- Ruby Vector component()用法及代碼示例
- Ruby Vector independent?用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby | Vector round() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。