Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to add units #151

Open
ybart opened this issue Oct 20, 2017 · 0 comments
Open

Ability to add units #151

ybart opened this issue Oct 20, 2017 · 0 comments

Comments

@ybart
Copy link

ybart commented Oct 20, 2017

Hi, in our application, we need to generate words from number including an unit.

I've done the following quick-and-dirty patch which works for us, but would need more work to be included in this gem.

I'm including it here for anyone interested :

module NumbersAndWords
  module Wrappers
    class Float
      ZERO_SYMBOL = '0'.freeze

      attr_accessor :number

      def initialize(number)
        @number = number
      end

      def to_words(options = {})
        @options = options
        words = []
        words << add_unit_to(integral_part_with(options), options)
        words << fractional_part_with(options) unless fractional_part_is_nil?
        NumbersAndWords::WordsArray.new(words).join options
      end

      private

      def add_unit_to(words, options)
        return unless options[:unit]

        i18n_options = { count: number.abs.floor, scope: :numbers }

        words_array = words.split(' ')
        words_array << ::I18n.t(options[:unit], i18n_options)
        words_array.join(' ')
      end
    end
  end
end

Caveats that might need to be addressed for a PR:

  • Hard-coded separator.
  • Hard coded position.
  • Translation scope is hard-coded
  • Does not work for integer
  • Translation should be moved to the appropriate module
  • No tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant