dbTalk Databases Forums  

Rounding Up To Next .25 Increment: Alternative To IIF() ?

comp.databases.ms-access comp.databases.ms-access


Discuss Rounding Up To Next .25 Increment: Alternative To IIF() ? in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
(PeteCresswell)
 
Posts: n/a

Default Rounding Up To Next .25 Increment: Alternative To IIF() ? - 10-08-2010 , 09:15 AM






I've got a bunch of bond rating weights from 0 to 7 in increments
of .25.

My input file has decimal numbers from 0 to 7, but not in
increments of .25. e.g. 1.3, 2.1, and so-forth.

I need to convert those numbers to the next highest number in the
..25 increment scheme.

The only thing that comes to mind is a monster IIF() in the
spirit of:


Weight_Final_Omnibus:
IIf([weight_raw_omnibus]>7,999,IIf([weight_raw_omnibus] Between
6.7500001 And 7,7,IIf([weight_raw_omnibus] Between 6.500001 And
6.75,6.75,???)))


But I have a feeling that when I nest that IIF statement out to
handle all 29-or-whatever ranges I'm going to provoke the dreaded
"Expression too complex".

But even if that does not happen, the resulting expression (which
I'm plugging away on right now...) is going to be ugly - to read,
to debug, and to modify.

Can anybody suggest a cleaner and more scalable alternative?

Maybe some SQL or VBA syntax where it rounds a value up to the
next .25 increment?
--
PeteCresswell

Reply With Quote
  #2  
Old   
paii, Ron
 
Posts: n/a

Default Re: Rounding Up To Next .25 Increment: Alternative To IIF() ? - 10-08-2010 , 09:30 AM






Define a function to do the rounding.

'--------------------
' Round to nearest .5
'
' dValueToRound number to be rounded only accepts numbers > 0
'
Public Function Round5(ByVal dValueToRound As Double) As Double

Dim dFraction As Double ' fractional part of dValueToRound

Round5 = dValueToRound
If dValueToRound <= 0 Then
Exit Function
End If

dFraction = dValueToRound - Fix(dValueToRound)

If dFraction < 0.25 Then ' Round down to 2 dec
dFraction = Format(dFraction, "0.00")
Else
If dFraction < 0.7 Then ' Round down to .5
dFraction = 0.5
Else
dFraction = 1 ' Round up to 1
End If
End If

Round5 = Fix(dValueToRound) + dFraction

End Function

"(PeteCresswell)" <x@y.Invalid> wrote

Quote:
I've got a bunch of bond rating weights from 0 to 7 in increments
of .25.

My input file has decimal numbers from 0 to 7, but not in
increments of .25. e.g. 1.3, 2.1, and so-forth.

I need to convert those numbers to the next highest number in the
.25 increment scheme.

The only thing that comes to mind is a monster IIF() in the
spirit of:


Weight_Final_Omnibus:
IIf([weight_raw_omnibus]>7,999,IIf([weight_raw_omnibus] Between
6.7500001 And 7,7,IIf([weight_raw_omnibus] Between 6.500001 And
6.75,6.75,???)))


But I have a feeling that when I nest that IIF statement out to
handle all 29-or-whatever ranges I'm going to provoke the dreaded
"Expression too complex".

But even if that does not happen, the resulting expression (which
I'm plugging away on right now...) is going to be ugly - to read,
to debug, and to modify.

Can anybody suggest a cleaner and more scalable alternative?

Maybe some SQL or VBA syntax where it rounds a value up to the
next .25 increment?
--
PeteCresswell

Reply With Quote
  #3  
Old   
Clif McIrvin
 
Posts: n/a

Default Re: Rounding Up To Next .25 Increment: Alternative To IIF() ? - 10-08-2010 , 09:41 AM



Off the cuff thinking before Ron's offering came through (air code):

Public function Round25(ByValue vGivenValue as Variant) as Currency
Dim cIntermediate As Currency
cIntermediate = (vGivenValue * 4) + 0.999996
Round25 = Fix( cIntermediate ) / 4
End Function

May give you something to tinker with.


"paii, Ron" <none (AT) no (DOT) com> wrote

Quote:
Define a function to do the rounding.

'--------------------
' Round to nearest .5
'
' dValueToRound number to be rounded only accepts numbers > 0
'
Public Function Round5(ByVal dValueToRound As Double) As Double

Dim dFraction As Double ' fractional part of dValueToRound

Round5 = dValueToRound
If dValueToRound <= 0 Then
Exit Function
End If

dFraction = dValueToRound - Fix(dValueToRound)

If dFraction < 0.25 Then ' Round down to 2 dec
dFraction = Format(dFraction, "0.00")
Else
If dFraction < 0.7 Then ' Round down to .5
dFraction = 0.5
Else
dFraction = 1 ' Round up to 1
End If
End If

Round5 = Fix(dValueToRound) + dFraction

End Function

"(PeteCresswell)" <x@y.Invalid> wrote in message
news:999ua6ltp3a96k2jtaa5kc9oba5l9dse11 (AT) 4ax (DOT) com...
I've got a bunch of bond rating weights from 0 to 7 in increments
of .25.

My input file has decimal numbers from 0 to 7, but not in
increments of .25. e.g. 1.3, 2.1, and so-forth.

I need to convert those numbers to the next highest number in the
.25 increment scheme.

The only thing that comes to mind is a monster IIF() in the
spirit of:


Weight_Final_Omnibus:
IIf([weight_raw_omnibus]>7,999,IIf([weight_raw_omnibus] Between
6.7500001 And 7,7,IIf([weight_raw_omnibus] Between 6.500001 And
6.75,6.75,???)))


But I have a feeling that when I nest that IIF statement out to
handle all 29-or-whatever ranges I'm going to provoke the dreaded
"Expression too complex".

But even if that does not happen, the resulting expression (which
I'm plugging away on right now...) is going to be ugly - to read,
to debug, and to modify.

Can anybody suggest a cleaner and more scalable alternative?

Maybe some SQL or VBA syntax where it rounds a value up to the
next .25 increment?
--
PeteCresswell




--
Clif McIrvin

Change nomail.afraid.org to gmail.com to reply by email.
(nomail.afraid.org has been set up specifically for
use in usenet. Feel free to use it yourself.)

Reply With Quote
  #4  
Old   
(PeteCresswell)
 
Posts: n/a

Default Re: Rounding Up To Next .25 Increment: Alternative To IIF() ? - 10-08-2010 , 09:43 AM



Per paii, Ron:
Quote:
a function to do the rounding.
Thanks.

I'm going to go that route until somebody comes up with some SQL
syntax.

Create a work table.

Iterate through it....

Or maybe even try embedding the call do Round5 into a query.
--
PeteCresswell

Reply With Quote
  #5  
Old   
(PeteCresswell)
 
Posts: n/a

Default Re: Rounding Up To Next .25 Increment: Alternative To IIF() ? - 10-08-2010 , 10:16 AM



Per (PeteCresswell):
Quote:
I'm going to go that route until somebody comes up with some SQL
syntax.
Forget the SQL syntax. It runs plenty fast embedded in a query
- with no prospects of greatly-increased data volume.
--
PeteCresswell

Reply With Quote
  #6  
Old   
Phil
 
Posts: n/a

Default Re: Rounding Up To Next .25 Increment: Alternative To IIF() ? - 10-08-2010 , 11:01 AM



On 08/10/2010 15:43:12, "(PeteCresswell)" wrote:
Quote:
Per paii, Ron:
a function to do the rounding.

Thanks.

I'm going to go that route until somebody comes up with some SQL
syntax.

Create a work table.

Iterate through it....

Or maybe even try embedding the call do Round5 into a query.
Don't know if this helps

Function RoundIt(NoIn As Variant, Nearest As Single, UpDown As Boolean) As
Variant

Dim CurNoIn As Currency
Dim Quotient As Currency
Dim CurNearest As Currency
Dim Result As Currency
Dim Remainder As Currency
Dim DP As Integer

If InStr(Nearest, ".") = 0 Then
DP = 0
Else
' Access error in that len(.012) is reported as 4 although
' Nearest value is shown as 0.012
DP = Len(Right(Nearest, Len(CStr(Nearest)) - InStr(Nearest, ".")))
End If

CurNoIn = CCur(NoIn)
CurNearest = CCur(Nearest)
Quotient = CurNoIn / CurNearest
Result = Int(Quotient) * Nearest
Remainder = Round(Quotient - Int(Quotient), 6)
If UpDown = True Then
If Remainder > 0 Then
Result = Result + CurNearest
End If
End If

RoundIt = Round(Result, DP)

End Function

Phil

Reply With Quote
  #7  
Old   
John Spencer
 
Posts: n/a

Default Re: Rounding Up To Next .25 Increment: Alternative To IIF() ? - 10-08-2010 , 01:35 PM



Pardon me, but here is a simple expression that should work for you.
-Int(-[weight_raw_omnibus] * 4)/4

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Reply With Quote
  #8  
Old   
paii, Ron
 
Posts: n/a

Default Re: Rounding Up To Next .25 Increment: Alternative To IIF() ? - 10-08-2010 , 02:00 PM



That is so much better then my function

"John Spencer" <JSPENCER (AT) Hilltop (DOT) umbc> wrote

Quote:
Pardon me, but here is a simple expression that should work for you.
-Int(-[weight_raw_omnibus] * 4)/4

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Reply With Quote
  #9  
Old   
(PeteCresswell)
 
Posts: n/a

Default Re: Rounding Up To Next .25 Increment: Alternative To IIF() ? - 10-08-2010 , 02:23 PM



Per John Spencer:
Quote:
Pardon me, but here is a simple expression that should work for you.
-Int(-[weight_raw_omnibus] * 4)/4
Lovely.... !
--
PeteCresswell

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.