Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Time#to_f

time.to_f → float

Returns the value of time as a floating point number of seconds since the Epoch. The return value approximate the exact value in the Time object because floating point numbers cannot represent all rational numbers exactly.

Example
t = Time.now        #=> 2020-07-20 22:00:29.38740268 +0900
t.to_f              #=> 1595250029.3874028
t.to_i              #=> 1595250029

Note that IEEE 754 double is not accurate enough to represent the exact number of nanoseconds since the Epoch. (IEEE 754 double has 53bit mantissa. So it can represent exact number of nanoseconds only in ‘2 ** 53 / 1_000_000_000 / 60 / 60 / 24 = 104.2` days.) When Ruby uses a nanosecond-resolution clock function, such as clock_gettime of POSIX, to obtain the current time, Time#to_f can lost information of a Time object created with Time.now.

See also #to_datetime #to_i #to_date #to_json also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1