Sha256: 8431c26a23179bef8029998b5e8394c0cd49437db7d41ebb3566c4e2182b5882
Contents?: true
Size: 1.28 KB
Versions: 2
Compression:
Stored size: 1.28 KB
Contents
module CoreExt module NumberHelper class NumberToCurrencyConverter < NumberConverter # :nodoc: self.namespace = :currency def convert number = self.number.to_s.strip format = options[:format] if is_negative?(number) format = options[:negative_format] number = absolute_value(number) end rounded_number = NumberToRoundedConverter.convert(number, options) format.gsub('%n'.freeze, rounded_number).gsub('%u'.freeze, options[:unit]) end private def is_negative?(number) number.to_f.phase != 0 end def absolute_value(number) number.respond_to?(:abs) ? number.abs : number.sub(/\A-/, '') end def options @options ||= begin defaults = default_format_options.merge(i18n_opts) # Override negative format if format options are given defaults[:negative_format] = "-#{opts[:format]}" if opts[:format] defaults.merge!(opts) end end def i18n_opts # Set International negative format if it does not exist i18n = i18n_format_options i18n[:negative_format] ||= "-#{i18n[:format]}" if i18n[:format] i18n end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
core_ext-0.0.6 | lib/core_ext/number_helper/number_to_currency_converter.rb |
core_ext-0.0.5 | lib/core_ext/number_helper/number_to_currency_converter.rb |