This content has been marked as final.
Show 6 replies

1. Re: rounding
Newsgroup_User Mar 26, 2009 10:21 AM (in response to Nick201)Nick201 wrote:
> I would like to do round. What function i should use in order to get this
> results.
Your going to have to write your own of find one that somebody else has
written to this kind of strange 'quarter' rounding. That is not normal,
mathematical rounding that the CFML functions or going to do.
But you have a pretty good start there, figure out the rules and
exceptions and then you just need to code a function to uses the data
with these rules to return the desired results. 
2. Re: rounding
Newsgroup_User Mar 27, 2009 9:38 AM (in response to Nick201)Should be simple, try the following:
<cfset quarterRound = round(originalNumber*4)/4>
Chris.
Nick201 wrote:
> I would like to do round. What function i should use in order to get this
> results.
> .01  .24  0
> .25  .50  0.5
> .51  .74  0.5
> .75  1.0  1
> 1.01  1.24  1
> 1.25  1.50  1.5
> 1.51 1.74  1.5
> 1.75  2.0  2
>
> 
3. rounding
BKBK Mar 28, 2009 4:19 AM (in response to Nick201)Should be simple, try the following:
<cfset quarterRound = round(originalNumber*4)/4>
You can immediately see that this is wrong. You're dividing integers by 4 and so will get numbers ending in .25. These are incorrect, as the result must be a whole number or a whole number and a half.
Here is a possible answer. First, think of the number of whole quarters in a number. For example, there are 2 whole quarters in 0.749, but 3 in 0.75 and 3 in 0.99. The number of whole quarters in the list is as follows:
.01  .24 > 0
.25  .50 > 1  2
.51  .74 > 2
.75  1.0 > 3  4
1.01  1.24 > 4
1.25  1.50 > 5  6
1.51 1.74 > 6
1.75  2.0 > 7  8
The formula for the number of whole quarters in the number x is
fix(x*4)
Int(x*4) is good, too, but we'll go with fix(). We now seek a function which we can apply to these whole numbers N to get the required results. A possibility is
round(N/2)*0.5
When we apply it to the list of whole numbers, we get
0 > 0
1  2 > 0.5
3  4 > 1
5  6 > 1.5
7  8 > 2
You will see that that is the required result. What we therefore need is the composite of both functions. It is
round(fix(originalNumber*4)/2)*0.5

4. Re: rounding
Newsgroup_User Mar 28, 2009 4:58 AM (in response to Nick201)Yes, you are right, I copied some code that I had used that required
quarterrounding.
The required code should be
<cfset halfRound = round(originalNumber*2)/2>
The following test show it works.
<cfoutput>
<table>
<cfloop from="0" to="1.5" step="0.01" index="iA">
<tr>
<td>#iA#</td>
<td>#round(iA*2)/2#</td>
</tr>
</cfloop>
</table>
</cfoutput>
BKBK wrote:
> Should be simple, try the following:
> <cfset quarterRound = round(originalNumber*4)/4>
>
> You can immediately see that this is wrong. You're dividing by 4 and so will
> get numbers ending in .25. These are incorrect, as the result must be a whole
> number or a whole number and a half.
>
> Here is a possible answer. First, think of the number of whole quarters in a
> number. For example, there are 2 whole quarters in 0.749, but 3 in 0.75 and 3
> in 0.99. The number of whole quarters in the list is as follows:
>
> .01  .24 > 0
> .25  .50 > 1  2
> .51  .74 > 2
> .75  1.0 > 3  4
> 1.01  1.24 > 4
> 1.25  1.50 > 5  6
> 1.51 1.74 > 6
> 1.75  2.0 > 7  8
>
> The formula for the number of whole quarters in the number n is
>
> fix(x*4)
> [int(x*4) is good, too, but we'll go with fix()]
>
> We now seek a function which we can apply to these whole numbers N to get the
> required results. A possibility is
>
> round(N/2)*0.5
>
> When we apply it to the list of whole numbers, we get
>
> 0 > 0
> 1  2 > 0.5
> 3  4 > 1
> 5  6 > 1.5
> 7  8 > 2
>
> You will see that that is the required result. What we therefore need is the
> composite of both functions. It is
>
> round(fix(originalNumber*4)/2)*0.5
>
>
>
>
>
>
> 
5. Re: rounding
BKBK Mar 28, 2009 11:06 AM (in response to Nick201)Chris,
I prefer your round(originalNumber*2)/2 to my round(fix(originalNumber*4)/2)*0.5. It involves just one function, and so is simpler.

6. Re: rounding
BKBK Mar 28, 2009 11:24 AM (in response to Nick201)Others
fix(2*originalNumber+0.5)/2
int(2*originalNumber+0.5)/2