Solved

Round to nearest dollar & half dollar

Posted on 2007-04-03
14
1,394 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

777 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