Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / September 2004



Tip: Looking for answers? Try searching our database.

Rounding Decimals

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nicetim - 30 Sep 2004 04:09 GMT
Hi,
 I have searched through here and cflib and cant find a solution to this that
works.
I know its a simple math problem but i am to dumb to figure it out and was
hoping someone else had this same problem and if so a solution.

I have a system that calculates time in hours. as in 7.34 hours.
our finance dept dosnt want the final hours in this format. They want them
rounded to the neares quater hour
as in 7.25, 7.5, 7.75 et...

I found several custom tags that say they round to the nearst x number
specified, but all they end up doing is choping off the last decimal place.
I could do this with a ton of CFIF statements but that would just be ugly.
any help would be appricitated.

Tim
ayhanyildiz - 30 Sep 2004 04:15 GMT
there is not another way to do. you must use cfif or you may use cfcase which looks more neat
Johan - 30 Sep 2004 05:31 GMT
Why not use something like this - uses a multiplication/division:

http://www.cflib.org/udf.cfm?ID=136
OldCFer - 30 Sep 2004 21:51 GMT
Try this:

<cfset TheTime = 7.10>
<cfset QtrTime = Int(TheTime * 100)>
<cfset idx = QtrTime mod 25>
<cfif idx GT 12>
<cfset add = 1>
<cfelse>
<cfset add = -1>
</cfif>

<cfloop condition="#idx#">
<cfset QtrTime = QtrTime + Add>
<cfset idx = QtrTime mod 25>
</cfloop>
<cfset QtrTime = QtrTime/100>
<cfoutput>#NumberFormat(QtrTime,"99.99")#</cfoutput>
Richard Wilde - 30 Sep 2004 11:11 GMT
If you want to round down to the nearest hour (which I suspect will suit the
finance dept and not the workers!) then

<cfset hrs = '7.34'>
<cfset aNum = listToArray(hrs,'.')>
<cfoutput>#hrs#=#aNum[1]#.#int(anum[2] / 25) * 25#</cfoutput>

Simply
Get the decimal bit and divide by 25 turn into int and multiple by 25.

Richard

> Hi,
>  I have searched through here and cflib and cant find a solution to this
[quoted text clipped - 15 lines]
>
> Tim
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.