本文簡要介紹ruby語言中 Kernel.Float
的用法。
用法
Float(arg, exception: true) → float or nil
返回轉換為浮點數的 arg
。 Numeric
類型直接轉換,除了 String
和 nil
其餘類型使用 arg
.to_f
轉換。使用無效字符轉換 String
將導致 ArgumentError
。轉換 nil
會生成 TypeError
。可以通過傳遞 exception: false
來抑製異常。
Float(1) #=> 1.0
Float("123.456") #=> 123.456
Float("123.0_badstring") #=> ArgumentError: invalid value for Float(): "123.0_badstring"
Float(nil) #=> TypeError: can't convert nil into Float
Float("123.0_badstring", exception: false) #=> nil
相關用法
- Ruby Kernel.local_variables用法及代碼示例
- Ruby Kernel.Integer用法及代碼示例
- Ruby Kernel.binding用法及代碼示例
- Ruby Kernel.frozen?用法及代碼示例
- Ruby Kernel.`cmd`用法及代碼示例
- Ruby Kernel.autoload用法及代碼示例
- Ruby Kernel.loop用法及代碼示例
- Ruby Kernel.Hash用法及代碼示例
- Ruby Kernel.caller用法及代碼示例
- Ruby Kernel.set_trace_func用法及代碼示例
- Ruby Kernel.exit!用法及代碼示例
- Ruby Kernel.trap用法及代碼示例
- Ruby Kernel.String用法及代碼示例
- Ruby Kernel.select用法及代碼示例
- Ruby Kernel.syscall用法及代碼示例
- Ruby Kernel.then用法及代碼示例
- Ruby Kernel.sprintf用法及代碼示例
- Ruby Kernel.Pathname用法及代碼示例
- Ruby Kernel.srand用法及代碼示例
- Ruby Kernel.yield_self用法及代碼示例
- Ruby Kernel.BigDecimal用法及代碼示例
- Ruby Kernel.raise用法及代碼示例
- Ruby Kernel.test用法及代碼示例
- Ruby Kernel.pretty_inspect用法及代碼示例
- Ruby Kernel.format用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Kernel.Float。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。