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
- .new(options) ⇒ RoundingHelper constructor
Instance Attribute Summary
- #options readonly
Instance Method Summary
Constructor Details
.new(options) ⇒ RoundingHelper
# File 'activesupport/lib/active_support/number_helper/rounding_helper.rb', line 8
def initialize( ) @options = end
Instance Attribute Details
#options (readonly)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/rounding_helper.rb', line 6
attr_reader :
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 [:significant] && [:precision] > 0 [:precision] - digit_count(convert_to_decimal(number)) else [: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) + [: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, .fetch(:round_mode, :default).to_sym) rounded_number.zero? ? rounded_number.abs : rounded_number # prevent showing negative zeros end