Skip to main content
March 29, 2008
Question

Rounding... sort of

  • March 29, 2008
  • 9 replies
  • 746 views
Hello everyone. I am trying to take a number and round it down. For example, I want to change 38 to 30 or 32 to 30 or 59 to 50. Anyone know how to do that?
    This topic has been closed for replies.

    9 replies

    Inspiring
    April 1, 2008
    BKBK wrote:
    > Ian Skinner wrote:
    > Why fix() over int()?
    >
    > Just personal preference. After all fix() and int() are basically equivalent. Mathematically.
    >
    >

    Basically but not quite the same. Which can lead one into a lot of
    trouble if one does assume they are in fact the same.

    BKBK
    Community Expert
    Community Expert
    April 2, 2008
    Ian Skinner wrote about fix() and int():
    not quite the same

    True. However, I didn't say they are the same. I said they are mathematically equivalent.

    In all the programming languages I know, there are relationships between fix() and int() that ensure that, whatever you can do with fix(), you can also do with int(). For example, I found these relationships in CFML

    fix(x) = sgn(x) * int(abs(x))
    int(x) = sgn(x)*fix(abs(x)) + sgn(sgn(x-fix(x))+1) - 1


    BKBK
    Community Expert
    Community Expert
    April 1, 2008
    Ian Skinner wrote:
    Why fix() over int()?

    Just personal preference. After all fix() and int() are basically equivalent. Mathematically. The point of my post is the trick to divide and multiply by 10.



    Inspiring
    April 1, 2008
    BKBK wrote:
    > Idesdema wrote:
    > Sounds like rounding down to the nearest ten. Then there is one function
    > tailor-made for it: fix().

    Why fix() over int()? I was about to suggest the old school int()
    solution which is basically exactly the same as your fix() version.

    <cfoutput>#int(38/10)*10#</cfoutput>

    > <cfset x=38>
    > <cfoutput>#fix(x/10)*10#</cfoutput>

    > Similarly, to round down to nearest dozen:
    >
    > <cfset x=38>
    > <cfoutput>#fix(x/12)*12#</cfoutput>
    <cfoutput>#int(x/12)*12#</cfoutput>
    >
    > To round down to nearest score:
    >
    > <cfset x=38>
    > <cfoutput>#fix(x/20)*20#</cfoutput>
    <cfoutput>#int(x/20)*20#</cfoutput>
    Inspiring
    April 1, 2008
    I think it depends on the desired results when the number is negative. Or if negative values are even a factor here.

    <cfset mult = 10>
    <cfset x = 38>
    <cfoutput>
    1. fix = #(fix(x/mult)*mult)#<br>
    1. int = #(int(x/mult)*mult)#<br>
    1. mod = #(x - (x mod mult))#<hr>
    </cfoutput>

    <cfset mult = 10>
    <cfset x = -38>
    <cfoutput>
    2. fix = #(fix(x/mult)*mult)#<br>
    2. int = #(int(x/mult)*mult)#<br>
    2. mod = #(x - (x mod mult))#<br>
    </cfoutput>

    Results:
    1. fix = 30
    1. int = 30
    1. mod = 30
    --------------------------------------------------------------------------------
    2. fix = -30
    2. int = -40
    2. mod = -30
    BKBK
    Community Expert
    Community Expert
    April 1, 2008
    Idesdema wrote:
    I am trying to take a number and round it down. For example, I want to change 38 to 30 or 32 to 30 or 59 to 50.

    Sounds like rounding down to the nearest ten. Then there is one function tailor-made for it: fix().

    <cfset x=38>
    <cfoutput>#fix(x/10)*10#</cfoutput>

    ----------------- you may ignore what follows --------------

    Similarly, to round down to nearest dozen:

    <cfset x=38>
    <cfoutput>#fix(x/12)*12#</cfoutput>

    To round down to nearest score:

    <cfset x=38>
    <cfoutput>#fix(x/20)*20#</cfoutput>

    and so on.



    April 1, 2008
    No because I figured it out prior to that reply. However if I decide to not be lazy I will try MOD.
    April 1, 2008
    That is correct. I did find a temporary solution to this but it's not clean and I know there's something better.
    Inspiring
    April 1, 2008
    Did you try using modulus? It seems to do what you are looking for.

    newNumber = origNumber - (origNumber MOD 10) .
    Inspiring
    April 1, 2008
    I think they want to round down to the nearest multiple of 10. In which case int would not suffice.
    Participating Frequently
    March 31, 2008
    The function to find the closest integer is "Int". But depending on your requirements you can figure out different techniques.

    Inspiring
    March 30, 2008
    Subtract modulus 10 from the number.

    ie
    38 - (38 MOD 10) ...OR
    38 - (8) = 30