SubName: Map a range of numbers from A-B to C-D and return the mapped value.
Description: I use B4R a lot and that package includes a function called MapRange. B4A doesn't have a version of MapRange and I couldn't find one on the forum anywhere, so I found the formula online and I put together this little sub. Using MapRange you can map any range of numbers to another different range of numbers. For example 0 to 100 can be mapped to -50 to 50, 25 to 75, 2000 to 5000, -300 to -150 or whatever number range you need to return a value from. Your original number range can be any range you wish and not just 0 to 100 like above.
If you need decimal results, just change these Ints to Floats.
Tags: maprange, map, range, from, to
Description: I use B4R a lot and that package includes a function called MapRange. B4A doesn't have a version of MapRange and I couldn't find one on the forum anywhere, so I found the formula online and I put together this little sub. Using MapRange you can map any range of numbers to another different range of numbers. For example 0 to 100 can be mapped to -50 to 50, 25 to 75, 2000 to 5000, -300 to -150 or whatever number range you need to return a value from. Your original number range can be any range you wish and not just 0 to 100 like above.
If you need decimal results, just change these Ints to Floats.
B4X:
'Map the value from the 'From' range A and B to the 'To' range C and D.
Sub MapRange(Value As Int, FromLow As Int, FromHigh As Int, ToLow As Int, ToHigh As Int) As Int
Return ToLow + (ToHigh - ToLow) * ((Value - FromLow) / (FromHigh - FromLow))
End Sub
Tags: maprange, map, range, from, to
Last edited: