Skip to main content
Mar 26, 2009 4:46 PM UTC
Question

rounding

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

6 replies

BKBK
Community Expert
Community Expert
Mar 28, 2009 6:24 PM UTC
Others
fix(2*originalNumber+0.5)/2
int(2*originalNumber+0.5)/2

Translate
BKBK
Community Expert
Community Expert
Mar 28, 2009 6:06 PM UTC
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.




Translate
Inspiring
Mar 28, 2009 11:58 AM UTC
Yes, you are right, I copied some code that I had used that required
quarter-rounding.
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
>
>
>
>
>
>
>
Translate
BKBK
Community Expert
Community Expert
Mar 28, 2009 11:19 AM UTC
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





Translate
Inspiring
Mar 27, 2009 4:38 PM UTC
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
>
>
Translate
Inspiring
Mar 26, 2009 5:21 PM UTC
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.
Translate