• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

RoundUp() function?

Status
Not open for further replies.
Level 10
Joined
Aug 19, 2008
Messages
491
Hey dudes! :grin:
Is there an easy way to round up a real number to the nearest tenth ten?
I.e 123.456 ~ 130
That would be totally epic, cause it will solve a very annoying bug in my project.
If there is no easy way I'll just have to implent it anyway :sad:

Help appreciated.
Cookie to first dude with proper solution :thumbs_up:

Solved!
by Shadow Deamon

Use this function to round a real
JASS:
library MyFunctions
    //call Round( real whichNumber, real whatNearest )
    function Round takes real number, real nearest returns real
        set number = R2I( number / nearest + 0.5 ) * nearest
        return number
    endfunction
endlibrary
 
Last edited:
Level 14
Joined
Nov 23, 2008
Messages
187
Hmm, didn't understand question well.
Do you mean, if you have 123.456 rounded up version would be 123.5? If so, rounding algorithm would look like this:

1. 123.456 + 0.5 * 10.0^(-1) = 123.506
2. 123.506 * 10.0^(1) = 1235.06
3. R2I(1235.06) = 1235
4. 1235 * 10.0^(-1) = 123.5

Right?
 
Level 10
Joined
Aug 19, 2008
Messages
491
Hmm, didn't understand question well.
Do you mean, if you have 123.456 rounded up version would be 123.5? If so, rounding algorithm would look like this:

1. 123.456 + 0.5 * 10.0^(-1) = 123.506
2. 123.506 * 10.0^(1) = 1235.06
3. R2I(1235.06) = 1235
4. 1235 * 10.0^(-1) = 123.5

Right?

No, I meant having 123.456 rounded up to 130.
Whooops sorry man I just realized my mistake :sad: Sorry for inconvinience
 
Level 14
Joined
Nov 23, 2008
Messages
187
Okay, if I'll use reverse coefficient, we get that formula(in short):

JASS:
set MyReal = R2I(MyReal / 10.0 + 0.5) * 10.0

  • Set MyReal = ((Real((Integer(((MyReal / 10.00) + 0.50))))) x 10.00)
For 123.456 it will return 120.000 (because "3" < "5", so it will round down)
For 456.789 it will return 460.000 (because "6" > "5", so it will round up)
 
Level 10
Joined
Aug 19, 2008
Messages
491
Seems to work
I extracted this function from it
JASS:
library Functions
    //call Round( real whichNumber, real whatNearest )
    function Round takes real number, real nearest returns real
        set number = R2I( number / nearest + 0.5 ) * nearest
        return number
    endfunction
endlibrary

Cookie as promised

SOLVED!
 
Last edited:
Status
Not open for further replies.
Top