round()是一個浮點類方法,它返回一個以n位十進製數字精度四舍五入到最接近值的浮點值。
用法:float.round()
參數:浮點值作為參數
返回:浮點值四舍五入到最接近的精度
如果precision為-ve:integer,且至少包含ndigits.abs後跟零
如果ndigits是+ ve:浮點數,否則為整數
範例1:
# Ruby code for round() method
# declaring float value
a = 0.767
# declaring float value
b = 2999.011
# rounding the float value
puts "rounding a:#{a.round}\n\n"
# rounding the float value
puts "rounding b:#{b.round}\n\n"
輸出:
rounding a:1 rounding b:2999
範例2:
# Ruby code for round() method
# declaring float value
a = 0.767
# declaring float value
b = 2999.011
# declaring float value
c = 2.0000
# rounding the float value
puts "round a:#{a.round(2)}\n\n"
# rounding the float value
puts "round b:#{b.round(-2)}\n\n"
# rounding the float value
puts "round c:#{c.round(0)}\n\n"
輸出:
round a:0.77 round b:3000 round c:2
相關用法
- Ruby Float eql()用法及代碼示例
- Ruby Float quo()用法及代碼示例
- Ruby Float nan?()用法及代碼示例
- Ruby Float arg()用法及代碼示例
- Ruby Float zero?()用法及代碼示例
- Ruby Float floor()用法及代碼示例
- Ruby Float magnitude()用法及代碼示例
- Ruby Float negative()用法及代碼示例
- Ruby Float divmod()用法及代碼示例
- Ruby Float modulo()用法及代碼示例
- Ruby Float angle()用法及代碼示例
- Ruby Float ceil()用法及代碼示例
- Ruby Float rationalize()用法及代碼示例
- Ruby Float fdiv()用法及代碼示例
- Ruby Float coerce()用法及代碼示例
注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby Float round() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。