Always Round Up in Visual Studio 2005

Okay Ive gone threw the forums many times and I have yet to find a clear beginner answer. I would like something that has any number that has a decimal to round up..no matter what the number/decimal is.

Sorry if I sound rude Im just a little bit annoyed and not being unable to understand a lot of this.



Answer this question

Always Round Up in Visual Studio 2005

  • Steve Wash

    Something like the following should work

    Module Module1

    Sub Main()
    Dim x = RoundUp(1.1)
    Dim y = RoundUp(1.9)
    Dim x1 = RoundUp(2.1)
    Dim y1 = RoundUp(2.9)

    End Sub


    Function RoundUp(ByVal Value As Double) As Long
    Return Fix(Value - (Value \ 1 <> Value))
    End Function

    End Module


  • Noah Nadeau

    Hans

    Thanks for the correction - I was just following Spotty's (bad) example, but that's no excuse.

    Quibbling a bit more:

    Shouldn't -8.4 round up to give -8, i.e. the nearest, highest number. So there is no need to check if the number is positive or negative, just return Ceiling(Value)

    Function RoundUp(ByVal Value As Double) As Double
    Return Math.Ceiling(Value)
    End Function
    Function RoundUp(ByVal Value As Decimal) As Decimal
    Return Math.Ceiling(Value)
    End Function

    Actually, as a Decimal widens to Double do you really need to overload the function.

    I TOTALLY agree about Option Strict - please tell Renee. I cannot understand why VB Express has it turned off by default.


  • yjacket2006

    Is there any coding i have to put into my forms or can i just leave that in a module and it will work automatically
  • kgs1951

    Math.Ceiling(value) is the way to go if you want to round up to the next higher integer.  If you want to use a decimal value, then multiply by 10 to get the value to the first place after the decimal point and then divide after using the Ceiling function:

            Dim rnum, num2 As Double
            Dim num As Double = 8.25
            rnum = Math.Ceiling(num)
            Console.WriteLine(rnum)     'will return 9
            num2 = num * 10
            rnum = Math.Ceiling(num2) / 10
            Console.WriteLine(rnum)     'will return 8.3

    To find the 2nd place after the decimal point, multiply and then divide by 100, etc.

     

    --------------------------------------------

    Option Strict should ALWAYS be turned on, configured that way in the IDE for all projects.  Many shops require it.  If you plan to share code with C# programmers or wish to learn C# at any time in the future, it is absolutely essential for Option Strict to be On.  C# is always very strict, even more so than VB with Option Strict On, so just get used to it.

    PS: Option Strict is off by default in all versions of VB, not just the Express version.

     

     

     

     



  • Dman82

    Okay so how do i call it
  • dindelus

     spotty wrote:

        Function RoundUp(ByVal Value As Double) As Long
            Return Fix(Value - (Value \ 1 <> Value))
        End Function

    End Module

    Roundup(8.4) return 9 - Correct

    Roundup(-8.4) returns -7  - Oops

    Try

        Function RoundUp(ByVal Value As Double) As Long
            Return Int(Value - (Value \ 1 <> Value))
        End Function


  • CalinMac

    Im sorry about not specifing and sorry to bug you but they are in group boxes...so what would I put

     

    EDIT Nevermind I figured it out thanks!!


  • Sai Kakarlamudi

    Okay I dont think you are getting the point. Here is an example of my coding...its a calculator for an MMORPG and the amount of in this case tree you have to cut doesnt always come to a normal number. I would like something for all the labels with the lbl in front to be rounded up when button 1 is clicked. These numbers arent always the same so i don't think something like wont work

    Dim X As Double

    X = lblNormal.Text

    RoundUp(X)

    X = -8.4

    MessageBox.Show(RoundUp(X))

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If txtLevel.Text = "2" Then

    Label16.Text = frmLevels.lbl2.Text - txtExp.Text

    lblNormal.Text = Label16.Text / 25

    lblLight.Text = Label16.Text / 32

    lblOak.Text = Label16.Text / 37.5

    lblMedium.Text = Label16.Text / 55

    lblWillow.Text = Label16.Text / 67.5

    lblTeak.Text = Label16.Text / 85

    lblDense.Text = Label16.Text / 80

    lblMaple.Text = Label16.Text / 100

    lblHollow.Text = Label16.Text / 82.5

    lblMahogany.Text = Label16.Text / 125

    lblYew.Text = Label16.Text / 175

    lblMagic.Text = Label16.Text / 250

    End If

    End Sub


  • Wil Burton

    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim X As Double
    X = 8.4
    MessageBox.Show(RoundUp(X))
    X = -8.4
    MessageBox.Show(RoundUp(X))
    End Sub
    End Class

    Public Module Module1
    Function RoundUp(ByVal Value As Double) As Long
    Return Int(Value - (Value \ 1 <> Value))
    End Function
    End Module


  • agney

    Quibbling a bit: the compiler will complain about this code with Option Strict On and it will generate an OverFlow exception for very large numbers (1E19). Avoid this with:

    Module Module1
    Function RoundUp(ByVal Value As Double) As Double
    If Value < 0 Then Return Math.Floor(Value) Else Return Math.Ceiling(Value)
    End Function
    Function RoundUp(ByVal Value As Decimal) As Decimal
    If Value < 0 Then Return Math.Floor(Value) Else Return Math.Ceiling(Value)
    End Function
    End Module

    Y'all don't use Option Strict On Pretty essential for Express starters...


  • nabeelfarid

    Solitaire,

    I've had the pleasure of reading your replies in this forum. I am a beginner. I am trying to create a calculator that has the basics and contains sin, cos, & tangent. Believe it or not my wall lies with the basic "+", "-", "*", "/", calculations. I'm using VB Studio 2005. I've managed to get the sin,cos, tan, Abs, decimal, +/-, 1/x, and % working. I figured they woul be the hardest. I tried using a function to return a value in my btnCalculate; for "+", "-", "*", and "/", nothin doing. I tried the code u suggested for express; nothing doing. Usually when pointed in the right direction I can figure out what to do. "Totally lost". Can send code if needed. Do u have other suggestions

    P.S. "Would prefer to send code FYEO = For Your Eyes Only


  • Zulbaric

    That's two religion wars in one post. How should a negative number round up and Option Strict. Neither one is winnable, it is a point of view. I'll avoid the rounding one but I'll take a stand on Option Strict. If you were a VB6 programmer, you want it off. If you're a "new to" .NET programmer, like most Express users are, it should be on. As long as we're posting in the Express forum, I'd invite the question answerers to vet their answers with Option Strict On. I've just never seen a good reason why it should be off. Ever.


  • project123

    If you put the function in a module you will be able to call it from any of your forms.


  • DarrellMerryweather

    You may have got a more appropriate answer if you had provided that information in your first post!

    If all your labels are on a form (not in panels or group boxes) then try this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For Each C As Control In Me.Controls
    If TypeOf C Is Label And C.Name.StartsWith("lbl") Then
    C.Text = Math.Ceiling(CDbl(C.Text)).ToString
    End If
    Next
    End Sub


  • Always Round Up in Visual Studio 2005