Sha256: 3a1974088d7d09febea8dfc6ca3b539a2691b824e4d6439a0c56991f15e374af

Contents?: true

Size: 1.84 KB

Versions: 52

Compression:

Stored size: 1.84 KB

Contents

require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes.rb', __FILE__)

# src.scan(/[+-]?[\d_]+\.[\d_]+(e[+-]?[\d_]+)?\b|[+-]?[\d_]+e[+-]?[\d_]+\b/i)

describe "String#to_f" do
  it "treats leading characters of self as a floating point number" do
   "123.45e1".to_f.should == 1234.5
   "45.67 degrees".to_f.should == 45.67
   "0".to_f.should == 0.0
   "123.45e1".to_f.should == 1234.5

   ".5".to_f.should == 0.5
   ".5e1".to_f.should == 5.0
   "5e".to_f.should == 5.0
   "5E".to_f.should == 5.0
  end

  it "treats special float value strings as characters" do
    "NaN".to_f.should == 0
    "Infinity".to_f.should == 0
    "-Infinity".to_f.should == 0
  end

  it "allows for varying case" do
    "123.45e1".to_f.should == 1234.5
    "123.45E1".to_f.should == 1234.5
  end

  it "allows for varying signs" do
    "+123.45e1".to_f.should == +123.45e1
    "-123.45e1".to_f.should == -123.45e1
    "123.45e+1".to_f.should == 123.45e+1
    "123.45e-1".to_f.should == 123.45e-1
    "+123.45e+1".to_f.should == +123.45e+1
    "+123.45e-1".to_f.should == +123.45e-1
    "-123.45e+1".to_f.should == -123.45e+1
    "-123.45e-1".to_f.should == -123.45e-1
  end

  it "allows for underscores, even in the decimal side" do
    "1_234_567.890_1".to_f.should == 1_234_567.890_1
  end

  it "returns 0 for strings with any non-digit in them" do
    "blah".to_f.should == 0
    "0b5".to_f.should == 0
    "0d5".to_f.should == 0
    "0o5".to_f.should == 0
    "0xx5".to_f.should == 0
  end

  it "takes an optional sign" do
    "-45.67 degrees".to_f.should == -45.67
    "+45.67 degrees".to_f.should == 45.67
    "-5_5e-5_0".to_f.should == -55e-50
    "-".to_f.should == 0.0
    (1.0 / "-0".to_f).to_s.should == "-Infinity"
  end

  it "returns 0.0 if the conversion fails" do
    "bad".to_f.should == 0.0
    "thx1138".to_f.should == 0.0
  end
end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
rhodes-7.6.0 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-7.5.1 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-7.4.1 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-7.1.17 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-6.2.0 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-6.0.11 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-5.5.18 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-5.5.17 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-5.5.15 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-5.5.0.22 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-5.5.2 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-5.5.0.7 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-5.5.0.3 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-5.5.0 spec/framework_spec/app/spec/core/string/to_f_spec.rb
tauplatform-1.0.3 spec/framework_spec/app/spec/core/string/to_f_spec.rb
tauplatform-1.0.2 spec/framework_spec/app/spec/core/string/to_f_spec.rb
tauplatform-1.0.1 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-3.5.1.12 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-3.3.5 spec/framework_spec/app/spec/core/string/to_f_spec.rb
rhodes-3.4.2 spec/framework_spec/app/spec/core/string/to_f_spec.rb