Sha256: a05d0a61df4c9d23efd0d214070d3a836e7ff705053519f0b372f21ae7eca140

Contents?: true

Size: 562 Bytes

Versions: 17

Compression:

Stored size: 562 Bytes

Contents

# frozen_string_literal: true

class ThinkingSphinx::FloatFormatter
  PATTERN = /(\d+)e\-(\d+)$/

  def initialize(float)
    @float = float
  end

  def fixed
    return float.to_s unless exponent_present?

    ("%0.#{decimal_places}f" % float).gsub(/0+$/, '')
  end

  private

  attr_reader :float

  def exponent_decimal_places
    float.to_s[PATTERN, 1].length
  end

  def exponent_factor
    float.to_s[PATTERN, 2].to_i
  end

  def exponent_present?
    float.to_s['e']
  end

  def decimal_places
    exponent_factor + exponent_decimal_places
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
thinking-sphinx-5.6.0 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-5.5.1 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-5.5.0 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-5.4.0 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-5.3.0 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-5.2.1 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-5.2.0 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-5.1.0 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-5.0.0 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-4.4.1 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-4.4.0 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-4.3.2 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-4.3.1 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-4.3.0 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-4.2.0 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-4.1.0 lib/thinking_sphinx/float_formatter.rb
thinking-sphinx-4.0.0 lib/thinking_sphinx/float_formatter.rb