本文簡要介紹ruby語言中 String.to_f
的用法。
用法
to_f → float
返回將 self
中的前導字符解釋為浮點數的結果:
'3.14159'.to_f # => 3.14159
'1.234e-2'.to_f # => 0.01234
超過前導有效數字的字符(在給定的 base
中)將被忽略:
'3.14 (pi to two places)'.to_f # => 3.14
如果沒有前導有效數字,則返回零:
'abcdef'.to_f # => 0.0
相關用法
- Ruby String.to_sym用法及代碼示例
- Ruby String.to_i用法及代碼示例
- Ruby String.to_c用法及代碼示例
- Ruby String.to_d用法及代碼示例
- Ruby String.to_r用法及代碼示例
- Ruby String.tr用法及代碼示例
- Ruby String.tr_s用法及代碼示例
- Ruby String.match?用法及代碼示例
- Ruby String.unpack用法及代碼示例
- Ruby String.scan用法及代碼示例
- Ruby String.dump用法及代碼示例
- Ruby String.oct用法及代碼示例
- Ruby String.size用法及代碼示例
- Ruby String.scrub用法及代碼示例
- Ruby String.chop用法及代碼示例
- Ruby String.bytesize用法及代碼示例
- Ruby String.count用法及代碼示例
- Ruby String.string <=>用法及代碼示例
- Ruby String.ascii_only?用法及代碼示例
- Ruby String.downcase用法及代碼示例
- Ruby String.capitalize用法及代碼示例
- Ruby String.length用法及代碼示例
- Ruby String.lines用法及代碼示例
- Ruby String.unicode_normalize用法及代碼示例
- Ruby String.center用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 String.to_f。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。