Solved

Round to nearest dollar & half dollar

Posted on 2007-04-03
14
1,372 Views
Last Modified: 2008-02-01
I've tried searching the solved questions to help me with this but nothing seemed to work... without editing that is, and my ability to edit code is quite minimal.  Here is what I have and need:

Have: $2.18  Need: $2.00
Have: $3.29  Need: $3.50
Have: $2.55  Need: $2.50
Have: $1.77  Need: $2.00

Basically:
0.00 - 0.25 = 0.00
0.26 - 0.75 = 0.50
0.76 - 0.99 = 1.00

Thanks in advanced for your help.
0
Comment
Question by:TBayXXXV
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 75
ID: 18844423
0
 
LVL 42

Expert Comment

by:dqmq
ID: 18844475
The general equation equation for that is

Round(Amount*/dollars),precision)*dollars

For our case, specifically, you want to round on  .50 dollars, so it becomes:
Round(2.18/.5,2)*.5 = 2.00
Round(3.29/.5,2)*.5 =3.5

So, do this:
Round(Amt/.5,2)*.5
0
 
LVL 75
ID: 18844723
btw ... in the thread I posted ... note that there are **several** ideas that don't quite work.

but .... look at cactus_data's solution (not accepted, but should have been) near the bottom of the post ... VERY ELEGANT, simple ... less is more.  This subject was complete hashed out in that thread.  Please check it out.

mx
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 18844951
Try this function on. Just put it in a module and then save it and call it.
-----------------------------------------------
Public Function RoundToHalf(InputAmt As Double) As Double

Dim InVal As String

InVal = Format(InputAmt, "0.00")

Select Case CInt(Right(InVal, 2))
    Case Is <= 25
        RoundToHalf = CDbl(Left(InVal, Len(InVal) - 2) & "00")
    Case 26 To 75
        RoundToHalf = CDbl(Left(InVal, Len(InVal) - 2) & "50")
    Case Is >= 76
        RoundToHalf = CDbl(Left(InVal, Len(InVal) - 3)) + 1
End Select

End Function
0
 

Author Comment

by:TBayXXXV
ID: 18845285
DatabaseMX: That is the one I originally tried to work with and like cactus_data's said, I'd much rather handle currency as numeric, but I can't get it to work the way I need it.  It rounds everything up, while I need some rounded down.  I am not very Code savvy so my attempts at adjusting code is a bit limited.
0
 
LVL 6

Expert Comment

by:twintai
ID: 18846622
I tested the following code I created and it worked almost perfectly.

If VBA.Mid(Me.Test1.Value, ((VBA.InStr(1, Me.Test1.Value, ".")) + 1), ((VBA.Len(Me.Test1.Value)) - (VBA.InStr(1, Me.Test1.Value, ".")))) <= 25 Then
    Me.Field.Value = VBA.Mid(Me.Test1.Value, 2, ((VBA.InStr(1, Me.Test1.Value, ".")) - 1)) & "00"
End If

If VBA.Mid(Me.Test1.Value, ((VBA.InStr(1, Me.Test1.Value, ".")) + 1), ((VBA.Len(Me.Test1.Value)) - (VBA.InStr(1, Me.Test1.Value, ".")))) > 25 And VBA.Mid(Me.Test1.Value, ((VBA.InStr(1, Me.Test1.Value, ".")) + 1), ((VBA.Len(Me.Test1.Value)) - (VBA.InStr(1, Me.Test1.Value, ".")))) <= 50 Then
    Me.Field.Value = VBA.Mid(Me.Test1.Value, 2, ((VBA.InStr(1, Me.Test1.Value, ".")) - 1)) & "50"
End If

If VBA.Mid(Me.Test1.Value, ((VBA.InStr(1, Me.Test1.Value, ".")) + 1), ((VBA.Len(Me.Test1.Value)) - (VBA.InStr(1, Me.Test1.Value, ".")))) > 50 And VBA.Mid(Me.Test1.Value, ((VBA.InStr(1, Me.Test1.Value, ".")) + 1), ((VBA.Len(Me.Test1.Value)) - (VBA.InStr(1, Me.Test1.Value, ".")))) <= 75 Then
    Me.Field.Value = VBA.Mid(Me.Test1.Value, 2, ((VBA.InStr(1, Me.Test1.Value, ".")) - 1)) & "75"
End If

If VBA.Mid(Me.Test1.Value, ((VBA.InStr(1, Me.Test1.Value, ".")) + 1), ((VBA.Len(Me.Test1.Value)) - (VBA.InStr(1, Me.Test1.Value, ".")))) > 75 Then
    Me.Field.Value = (VBA.Round(Me.Test1.Value, 0))
End If
0
 
LVL 75
ID: 18846697
OK ... here you go:

Let 'y' be your field.  For simplicity ...

y = Format(y, "0.00")

x = "." & Right(y, 2)

z = Int(y) + Switch(x > 0.00 And x <= 0.25, 0.00, x >= 0.26 And x <= 0.75, 0.5, x >= 0.76 And x <= 0.99, 1)

y= Format(z, "0.00")   >> converted value

mx
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 42

Expert Comment

by:dqmq
ID: 18846974
Once again, rounding to nearest half-dollar is this simple:

Round(YourAmt/.5,2)*.5

or, if this makes more sense:
Round(YourAmt * 2, 2) / 2

What am I missing?
0
 
LVL 42

Expert Comment

by:dqmq
ID: 18846992
Oh, #$%#@$, I just figured out what I'm missing. Try this:
Round(YourAmt*2, 0 ) / 2
0
 
LVL 75
ID: 18847024
oops:
dqmq

Round(0.75*2, 0 ) / 2 = 1.00,   not 0,50

Weird ... only that one case (0.75) fails !

"0.00 - 0.25 = 0.00
0.26 - 0.75 = 0.50
0.76 - 0.99 = 1.00"

mx
0
 
LVL 75
ID: 18847055
Seriously ... If you use the Switch() ... you can see that all of the 'boundary conditions' are covered ... there is no guess work ...it has to work.  And, you could adjust it for different scenarios.

mx
0
 
LVL 42

Accepted Solution

by:
dqmq earned 125 total points
ID: 18848137
>Round(0.75*2, 0 ) / 2 = 1.00,   not 0,50
>Weird ... only that one case (0.75) fails

OK, I totally missed that as .75 would normally round "up".  So, there's the revised formula:

Int((AMT + .24) * 2 ) / 2

I changed from Round to Int because I discovered VBA applies "bankers rounding"


0
 

Author Comment

by:TBayXXXV
ID: 18850744
Thanks all of you for your imput, I really learned a lot.
0
 
LVL 75
ID: 18851107
Glad to be of assistance to dqmq :-)

mx
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now