Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Rounding... sort of

Guest
Mar 29, 2008 Mar 29, 2008
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?
748
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 29, 2008 Mar 29, 2008
Subtract modulus 10 from the number.

ie
38 - (38 MOD 10) ...OR
38 - (8) = 30
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 31, 2008 Mar 31, 2008
The function to find the closest integer is "Int". But depending on your requirements you can figure out different techniques.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 31, 2008 Mar 31, 2008
I think they want to round down to the nearest multiple of 10. In which case int would not suffice.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 31, 2008 Mar 31, 2008
That is correct. I did find a temporary solution to this but it's not clean and I know there's something better.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 31, 2008 Mar 31, 2008
Did you try using modulus? It seems to do what you are looking for.

newNumber = origNumber - (origNumber MOD 10) .
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 31, 2008 Mar 31, 2008
No because I figured it out prior to that reply. However if I decide to not be lazy I will try MOD.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 31, 2008 Mar 31, 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.



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2008 Apr 01, 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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 01, 2008 Apr 01, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2008 Apr 01, 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.



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2008 Apr 01, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2008 Apr 01, 2008
LATEST
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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources