Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Float#to_s

() -> ::String
to_s → string

Returns a string containing a representation of self; depending of the value of self, the string representation may contain:

  • A fixed-point number.

    Example
    3.14.to_s         # => "3.14"
    
  • A number in “scientific notation” (containing an exponent).

    Example
    (10.1**50).to_s   # => "1.644631821843879e+50"
    
  • ‘Infinity’.

    Example
    (10.1**500).to_s  # => "Infinity"
    
  • ‘-Infinity’.

    Example
    (-10.1**500).to_s # => "-Infinity"
    
  • ‘NaN’ (indicating not-a-number).

    Example
    (0.0/0.0).to_s    # => "NaN"
    
See also #to_r #truncate #to_int #zero? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0