Skip to main content
Mar 29, 2008 9:21 PM UTC
Question

Rounding... sort of

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 is closed to new replies. Start a new post to keep the conversation going.

    12 replies

    Inspiring
    Apr 1, 2008 9:00 PM UTC
    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.

    Translate
    BKBK
    Community Expert
    Community Expert
    Apr 2, 2008 4:47 AM UTC
    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


    Translate
    BKBK
    Community Expert
    Community Expert
    Apr 1, 2008 6:52 PM UTC
    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.



    Translate
    Inspiring
    Apr 1, 2008 1:14 PM UTC
    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>
    Translate
    Inspiring
    Apr 1, 2008 1:54 PM UTC
    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
    Translate
    BKBK
    Community Expert
    Community Expert
    Apr 1, 2008 5:01 AM UTC
    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.



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

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

    Translate
    Inspiring
    Mar 30, 2008 3:09 AM UTC
    Subtract modulus 10 from the number.

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