Return to Snippet

Revision: 59570
at September 18, 2012 20:35 by slhck


Initial Code
class Numeric
  def scale_between(from_min, from_max, to_min, to_max)
    ((to_max - to_min) * (self - from_min)) / (from_max - from_min) + to_min
  end
end

Initial URL


Initial Description
Scales a number from a given range into a specific range, as explained [in this Stack Overflow](http://stackoverflow.com/questions/5294955/how-to-scale-down-a-range-of-numbers-with-a-known-min-and-max-value) post.

Just call it, for example like:

    0.5.scale_between(0, 1, 100, 200)

The result would be `150`.

Initial Title
Scale a number into a defined range

Initial Tags
number

Initial Language
Ruby