Posted By

slhck on 09/18/12


Tagged

number scale


Versions (?)

Scale a number into a defined range


 / Published in: Ruby
 

Scales a number from a given range into a specific range, as explained in this Stack Overflow post.

Just call it, for example like:

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

The result would be 150.

  1. class Numeric
  2. def scale_between(from_min, from_max, to_min, to_max)
  3. ((to_max - to_min) * (self - from_min)) / (from_max - from_min) + to_min
  4. end
  5. end

Report this snippet  

You need to login to post a comment.