rounding special case

ultimaultima Member, PRO Posts: 1,207

Hi guys, need a little help rounding numbers in a special case, and was wandering if i can get help from brilliant minds in the community

X.X number needs to be rounded down, to nearest .5 or rounded up depending on the decimal number.
for example.
.1, .2 gets rounded down to .0
.3 -.7 gets rounded to .5
.8, .9 gets rounded up.

i'm fiddling with mod and ceil / floor with no success.

thanks in advance.

Comments

  • SocksSocks London, UK.Member Posts: 12,822
    edited April 2016

    abs(round( X*2)/2)

  • SocksSocks London, UK.Member Posts: 12,822
    edited April 2016

    Explanation . . . . .

    X is the value you want to round.

    The abs function forces positive values, this is to account for the (odd) inclusion of the one negative value (-0.7).

  • ultimaultima Member, PRO Posts: 1,207

    so this will work for .3, .4 (round to .5) and .6 and .7 (rounded down to .5 as well)?
    will give it a try.. it'll be like math wizardry .. you guys are the best.

  • ultimaultima Member, PRO Posts: 1,207

    it works perfectly! thank you Socks
    as I said Wizardry!

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,879

    @Socks -- Nice solution!

  • SocksSocks London, UK.Member Posts: 12,822

    @RThurman said:
    @Socks -- Nice solution!

    Cheers ! :)

    Here's something odd that I don't really understand, when using roundTo(x,y) you will - as you know - get x with y decimal places.

    So . . . .

    roundTo (0.5,1) will give you 0.5
    roundTo (0.5,0) will give you 0

    . . . but when you round something to 0.5 places (I know, I know ! :) ) you get some, to me, counterintuitive results . . . for example:

    roundTo (0.5,0.5) will give you 0.63245553203368 - which is the square root of 0.4 !?
    roundTo (1,0.5) will give you 0.94868329805051 - which is the square root of 0.9 !?

    I know you're probably thinking 'don't mess with rounding values to non-existent decimal places, it's pointless and you will eventually go blind', but if you could explain it to me in less than 15 words I would be very thankful.

    :smile: :wink:

  • SocksSocks London, UK.Member Posts: 12,822

    @ultima said:
    it works perfectly! thank you Socks

    Good ! :)

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,879
    edited May 2016

    @Socks said:
    if you could explain it to me in less than 15 words I would be very thankful.

    Gulp!!
    Um.... rounding error?

  • SocksSocks London, UK.Member Posts: 12,822

    @RThurman said:
    Gulp!!
    Um.... rounding error?

    I think it might be aliens ?

Sign In or Register to comment.