Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# round UP to next biggest .5 or 0, e.g. 20.3-->20.5 & 28.8-->30, am i bound to a case statement? i tried good few functions already

Posted on 2004-10-07
Medium Priority
333 Views
hi, i need help on a round function and all it needs to do is always ROUND UP to the next half.

functions i've tried:
Private Function MyRound(ByVal aNumber As Single) As Single
If aNumber < 0 Then
Return CSng(Microsoft.VisualBasic.Fix((aNumber - 0.25) * 2) / 2)
Else
Return CSng(Microsoft.VisualBasic.Fix((aNumber + 0.25) * 2) / 2)
End If
End Function

Public Function Round5(ByVal d As Double) As Double
Return Math.Round(d * 2) / 2
End Function

am i bound to making a case statement, cause id absolutely learn nothing from it.
thanks!
0
Question by:jxharding
[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

LVL 25

Expert Comment

ID: 12246533
Hi jxharding,

a question

24.4 -> 20
25.0 -> 30

0

Author Comment

ID: 12246593
hi ronaldbiemans

it only needs to update to the next half, "28.8-->30" should be 28.8-->29.0
thus if the first decimal place is bigger than 0 and smaller than 5, the first decimal place must then be .5
else
if the first decimal is bigger than 5, then the non decimal must +1 and, the decimal must be .0.
thanks!

0

LVL 3

Accepted Solution

eekj earned 1000 total points
ID: 12246602
Dim d As Double = 20.3
MsgBox((Math.Round(Math.Round(d * 2), 0)) / 2.ToString)   'Displays 20.5

Dim e As Double = 28.8
MsgBox((Math.Round(Math.Round(e * 2), 0)) / 2.ToString)   'Displays 29
0

LVL 6

Assisted Solution

zulu_11 earned 1000 total points
ID: 12246700
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim dbnum As Double
dbnum = dbnum.Parse(tbNum.Text)
If Math.Round(dbnum) < dbnum Then
dbnum = Math.Round(dbnum) + 0.5
Else
dbnum = Math.Round(dbnum)
End If
MsgBox(dbnum)
End Sub

i tested the above function for the following

28.1 - 28.5 --> 28.5
28.6 - 28.9 --> 29

hope this helps

Zulu
0

LVL 6

Expert Comment

ID: 12246708
oh! and the tbNum is a text field in which i take an input of the number to test...
0

## Featured Post

Question has a verified solution.

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

XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
###### Suggested Courses
Course of the Month9 days, 7 hours left to enroll