Math problem - go to nearest #

chukky-jr

Member
Reaction score
15
so let's say i have an integer that will randomly generates #, but i want it went generated, has the value which are the multiplication of 5 (e.g 5/10/15/20/25 etc), but i'm stuck on making the formula, can anyone help me?

edited : i mean the steps are like these :
1. random value generated (let's say 13)
2. random value is used on a formula
3. formula returns the value rounded down (10)
 

Imp Midna

Active Member
Reaction score
52
If you always round down, its val = radomval - (randomval % 5). (% is modulo). If you also want to round up, i dont see how you would do it without an if though.
 

Tharius

Occasionally Around
Reaction score
39
If you also want to round up, i dont see how you would do it without an if though.

If you're talking about rounding it to the nearest multiple of 5, rather than always rounding up, it's possible to do without using an IF statement. Since WC3 converts reals to integers by simply truncating everything after the decimal point, you can force a nearest-number rounding by adding 0.5 to a real number, then converting it to an integer.

To round it to the nearest multiple of 5, first convert the integer value randomval to a real number, divide it by 5, and add 0.5, then convert it back to an integer and multiply by 5.

val = Integer(((Real(randomval) / 5.00) +0.50) x 5
 
General chit-chat
Help Users

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top