Class: ActiveSupport::NumberHelper::NumberToRoundedConverter
Do not use. This class is for internal use only.
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
NumberConverter
|
|
|
Instance Chain:
self,
NumberConverter
|
|
| Inherits: |
ActiveSupport::NumberHelper::NumberConverter
|
| Defined in: | activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb |
Constant Summary
-
COMMON_SEPARATOR_REGEXPS =
# File 'activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb', line 17{ "." => separator_regexps("."), "," => separator_regexps(","), }.freeze
NumberConverter - Inherited
Class Attribute Summary
NumberConverter - Inherited
Class Method Summary
NumberConverter - Inherited
| .convert, | |
| .convert_with_resolved_options | Used by a converter delegating to another one: the options it passes already hold every key of the defaults and of the locale's number.format, so only this converter's namespace is resolved again. |
| .i18n_format_key, .new | |
Instance Attribute Summary
NumberConverter - Inherited
Instance Method Summary
- #convert
- #format_number(number) private
- #strip_insignificant_zeros private
NumberConverter - Inherited
| #execute, #default_format_options, #default_value, #i18n_format_options, #namespace_format_options, | |
| #options | The format options, in increasing order of precedence: the defaults, the namespace's defaults, the locale's number.format, the locale's number. |
| #translate_in_locale, #translate_number_value_with_default, #valid_bigdecimal | |
Constructor Details
This class inherits a constructor from ActiveSupport::NumberHelper::NumberConverter
Class Method Details
.separator_regexps(separator)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb', line 12
def self.separator_regexps(separator) escaped_separator = Regexp.escape(separator) [/(#{escaped_separator})(\d*[1-9])?0+\z/, /#{escaped_separator}\z/].freeze end
Instance Method Details
#convert
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb', line 22
def convert helper = RoundingHelper.new() rounded_number = helper.round(number) if precision = [:precision] if [:significant] && precision > 0 digits = helper.digit_count(rounded_number) precision -= digits precision = 0 if precision < 0 # don't let it be negative end formatted_string = if rounded_number.finite? s = rounded_number.to_s("F") a, b = s.split(".", 2) if precision != 0 b << "0" * precision a << "." a << b[0, precision] end a else # Infinity/NaN "%f" % rounded_number end else formatted_string = rounded_number end delimited_number = NumberToDelimitedConverter.(formatted_string, ) format_number(delimited_number) end
#format_number(number) (private)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb', line 60
def format_number(number) if strip_insignificant_zeros insignificant_zeros, trailing_separator = COMMON_SEPARATOR_REGEXPS[[:separator]] || self.class.separator_regexps([:separator]) number.sub(insignificant_zeros, '\1\2').sub(trailing_separator, "") else number end end
#strip_insignificant_zeros (private)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb', line 56
def strip_insignificant_zeros [:strip_insignificant_zeros] end