123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::NumberHelper::RoundingHelper

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activesupport/lib/active_support/number_helper/rounding_helper.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(options) ⇒ RoundingHelper

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/rounding_helper.rb', line 8

def initialize(options)
  @options = options
end

Instance Attribute Details

#options (readonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/rounding_helper.rb', line 6

attr_reader :options

Instance Method Details

#absolute_precision(number) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/rounding_helper.rb', line 37

def absolute_precision(number)
  if options[:significant] && options[:precision] > 0
    options[:precision] - digit_count(convert_to_decimal(number))
  else
    options[:precision]
  end
end

#convert_to_decimal(number) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/rounding_helper.rb', line 26

def convert_to_decimal(number)
  case number
  when Float, String
    BigDecimal(number.to_s)
  when Rational
    BigDecimal(number, digit_count(number.to_i) + options[:precision])
  else
    number.to_d
  end
end

#digit_count(number)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/rounding_helper.rb', line 20

def digit_count(number)
  return 1 if number.zero?
  (Math.log10(number.abs) + 1).floor
end

#round(number)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/rounding_helper.rb', line 12

def round(number)
  precision = absolute_precision(number)
  return number unless precision

  rounded_number = convert_to_decimal(number).round(precision, options.fetch(:round_mode, :default).to_sym)
  rounded_number.zero? ? rounded_number.abs : rounded_number # prevent showing negative zeros
end