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

rounding

Guest
Mar 26, 2009 Mar 26, 2009

Copy link to clipboard

Copied

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
TOPICS
Advanced techniques

Views

550

Translate

Translate

Report

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 ,
Mar 26, 2009 Mar 26, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Mar 27, 2009 Mar 27, 2009

Copy link to clipboard

Copied

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
>
>

Votes

Translate

Translate

Report

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 28, 2009 Mar 28, 2009

Copy link to clipboard

Copied

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





Votes

Translate

Translate

Report

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 ,
Mar 28, 2009 Mar 28, 2009

Copy link to clipboard

Copied

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
>
>
>
>
>
>
>

Votes

Translate

Translate

Report

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 28, 2009 Mar 28, 2009

Copy link to clipboard

Copied

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.




Votes

Translate

Translate

Report

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 28, 2009 Mar 28, 2009

Copy link to clipboard

Copied

LATEST
Others
fix(2*originalNumber+0.5)/2
int(2*originalNumber+0.5)/2

Votes

Translate

Translate

Report

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
Documentation