Method: RSpec::Core::Formatters::Helpers.format_seconds

Defined in:
lib/rspec/core/formatters/helpers.rb

.format_seconds(float, precision = nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Formats seconds to have 5 digits of precision with trailing zeros removed if the number is less than 1 or with 2 digits of precision if the number is greater than zero.

The precision used is set in SUB_SECOND_PRECISION and DEFAULT_PRECISION.

Examples:

format_seconds(0.000006) #=> "0.00001"
format_seconds(0.020000) #=> "0.02"
format_seconds(1.00000000001) #=> "1"

Parameters:

  • float (Float)

Returns:

  • (String)

    formatted float

See Also:

  • #strip_trailing_zeroes
[View source]

60
61
62
63
64
65
# File 'lib/rspec/core/formatters/helpers.rb', line 60

def self.format_seconds(float, precision=nil)
  return '0' if float < 0
  precision ||= (float < 1) ? SUB_SECOND_PRECISION : DEFAULT_PRECISION
  formatted = "%.#{precision}f" % float
  strip_trailing_zeroes(formatted)
end