_plugins/eiti_format.rb
def percent(num, precision = 1, small = '&lt;1')
      if num.is_a? String
        num = num.to_f
      end
      if num.nil?
        # XXX: what should we represent null % as?
        return '--'
      # zero is zero
      elsif num.zero?
        return '0'
      # if it's less than 1, return the "small" representation
      elsif num < 1.0
        return small
      # if it has decimal precision, format it
      elsif num % 1 > 0
        return num.round(precision)
      end

      # puts "percent(#{num}, #{precision}, "#{small}")"
      num.to_i
    end
# format a number as a percentage with fixed precision, with an
# additionally configurable "small" placeholder to indicate values
# less than 1.
#
# >> EITI::Format.percent(10.5)
# => 10.5
# >> EITI::Format.percent(10.5, 0)
# => 11
# >> EITI::Format.percent(0.5)
# => '&lt;1'
# >> EITI::Format.percent(0.5, 0, '?')
# => '?'
format a number as a percentage with fixed precision, with an
additionally configurable "small" placeholder to indicate values
less than 1.


format a number as a percentage with fixed precision with an additionally configurable " small " placeholder to indicate values less than 1 .
def percent(num, precision = 1, small = '&lt;1')
      if num.is_a? String
        num = num.to_f
      end
      if num.nil?
        return '--'
      elsif num.zero?
        return '0'
      elsif num < 1.0
        return small
      elsif num % 1 > 0
        return num.round(precision)
      end
      num.to_i
    end
def percent ( num , precision = 1 , small = '&lt;1' ) if num . is_a? String num = num . to_f end if num . nil? return '--' elsif num . zero? return '0' elsif num < 1.0 return small elsif num % 1 > 0 return num . round ( precision ) end num . to_i end
format a number as a percentage with fixed precision, with an
additionally configurable "small" placeholder to indicate values
less than 1.
format a number as a percentage with fixed precision with an additionally configurable " small " placeholder to indicate values less than 1 .
# XXX: what should we represent null % as? # zero is zero # if it's less than 1, return the "small" representation # if it has decimal precision, format it # puts "percent(#{num}, #{precision}, "#{small}")"
num
precision
small