Sha256: 68a2121a0a58e9a350fea91831218c4fea8e1821f31bc11da94b9df6625516bc

Contents?: true

Size: 843 Bytes

Versions: 4

Compression:

Stored size: 843 Bytes

Contents

module CiteProc
  # A CiteProc Variable used for numeric values.
  class Number < Variable

	  MAX_ROMAN = 5000

    FACTORS = [
      ['m', 1000], ['cm', 900], ['d', 500], ['cd', 400],
      ['c',  100], ['xc',  90], ['l',  50], ['xl',  40],
      ['x',   10], ['ix',   9], ['v',   5], ['iv',   4],
      ['i',    1]
    ].freeze

		class << self
			def pluralize?(string)
				/\S\s*[,&-]\s*\S|\df/ === string
			end
			
			# @param number [#to_i] the number to convert
      # @return [String] roman equivalent of the passed-in number
      def romanize(number)
        number, roman = number.to_i, ''

        return number unless number > 0 || number < MAX_ROMAN

        FACTORS.each do |code, factor|
          count, number = number.divmod(factor)
          roman << (code * count)
        end

        roman
      end
		end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
citeproc-1.0.0.pre12 lib/citeproc/number.rb
citeproc-1.0.0.pre11 lib/citeproc/number.rb
citeproc-1.0.0.pre10 lib/citeproc/number.rb
citeproc-1.0.0.pre9 lib/citeproc/number.rb