當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby Vector round()用法及代碼示例

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]


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Ruby | Vector round() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。