るりまサーチ

最速Rubyリファレンスマニュアル検索!
11件ヒット [1-11件を表示] (0.224秒)
トップページ > クエリ:_builtin[x] > クエリ:*[x] > クエリ:to_f[x] > クラス:String[x]

別のキーワード

  1. _builtin new
  2. _builtin inspect
  3. _builtin []
  4. _builtin to_s
  5. _builtin each

ライブラリ

検索結果

String#to_f -> Float (26180.0)

文字列を 10 進数表現と解釈して、浮動小数点数 Float に変換します。

...by]{
p "-10".to_f # => -10.0
p "10e2".to_f # => 1000.0
p "1e-2".to_f # => 0.01
p ".1".to_f # => 0.1

p "1_0_0".to_f # => 100.0 # 数値リテラルと同じように区切りに _ を使える
p " \n10".to_f # => 10.0 # 先頭の空白・改行は無視される
p "7xa.5".to_f # => 7.0
//}...
...します。
変換対象が空文字列のケースでも、0.0 を返します。

//emlist[][ruby]{
p "".to_f # => 0.0
p "nan".to_f # => 0.0
p "INF".to_f # => 0.0
p "-Inf".to_f # => 0.0
//}

変換後の Float が有限の値を取れないときは、Float::INFINITY を用います。...
...uby -W2

p ("10" * 1000).to_f # => Infinity
# warning: Float 10101010101010101010... out of range
//}

なお、このメソッドとは逆に、数値を文字列に変換するには
Kernel.#sprintf, String#%, Integer#to_s
を使用します。

@see String#hex, String#oct, String#to_i,
Ker...