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
5
314 Views
Last Modified: 2008-02-01
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
Comment
Question by:jxharding
5 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12246533
Hi jxharding,

a question

24.4 -> 20
25.0 -> 30


0
 

Author Comment

by:jxharding
ID: 12246593
hi ronaldbiemans

sorry , i made a error in the heading.
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

by:
eekj earned 250 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

by:zulu_11
zulu_11 earned 250 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

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

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio - "Windows Form Designer generated code" 2 51
send messages to whatsapp programatically 2 48
vb.net help 4 31
Sending receiving text messages in vb.net 15 37
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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