Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Round to nearest dollar & half dollar

Posted on 2007-04-03
14
Medium Priority
?
1,505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +2
14 Comments
 
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
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.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

670 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