Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Array percentages of the maximum (See related posts)

Returns an array whose elements are the percentage of each element compared to the maximum element in the array.

>> [25, 150, 200, 5, 75, 125].percentages_of_maximum
=> [12, 75, 100, 2, 37, 62]


If you're using Rails, stick this in your "environment.rb" script in the "config" directory:

class Array
    def percentages_of_maximum
        self.collect{ |i| ((i.to_f / self.sort.last.to_f) * 100).to_i }
    end
end

You need to create an account or log in to post comments to this site.


Click here to browse all 4881 code snippets

Related Posts