locked
Show label in whole figures RRS feed

  • Question

  • Hi, I have a simple problem  which whatever I read on the forum or web doesn't appear to work .

    I have several labels which are the result of maths between two other boxes.. I only want to show this to the nearest whole figure..

    For example, one label returns something like 4.648290457625482 where either 4 or 5 would be ok.

    I would like to place a simple code that means that no TextBox or Label will read other than a whole number, if thats possible?  If not then a code to place controlling each answer label.

    So far I have had the message shown in the label I have attempted to show, has come back "False" or "".

    Many thanks if someone could wake me up, sure i've missed something simple. I am surprised there is nowhere in the label properties to control No. of places in decimal shown.

    Thanks

    PyrusBoy

    Tuesday, April 17, 2012 7:56 AM

Answers

  • Do you mean something like the Format feature?

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Val1 As String = CDbl(Me.TextBox1.Text)
            Dim Val2 As String = CDbl(Me.TextBox2.Text)
            Dim Result As Double = Val1 / Val2
            Me.Label2.Text = Result
            Me.Label3.Text = Format(Result, "N2") 'shows 2 decimals after . eg. 3.56 // N4 shows 4 decimals
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Label1.Text = " / "
        End Sub
    End Class

    You need:

    • 2 Textboxes
    • 3 Labels
    • 1 Button

    Copy my code to a new application an test is.
    For example try 5 in first Textbox an 6 in second one.

    Label2 will show 0.833333333 (the complete result)

    Label 3 will show 1 (mathematical rounded and formated as single number.
    In case you meant something else don't hesitate asking again.


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 and VB 2010 Express Be a good forum member - mark posts that contain the answers to your questions or those that are helpful. c# in vb Translator: http://www.developerfusion.com/tools/convert/csharp-to-vb/



    • Edited by Giftzwockel Tuesday, April 17, 2012 9:30 AM
    • Marked as answer by PyrusBoy Tuesday, April 17, 2012 9:31 AM
    Tuesday, April 17, 2012 9:25 AM

All replies

  • Do you mean something like the Format feature?

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Val1 As String = CDbl(Me.TextBox1.Text)
            Dim Val2 As String = CDbl(Me.TextBox2.Text)
            Dim Result As Double = Val1 / Val2
            Me.Label2.Text = Result
            Me.Label3.Text = Format(Result, "N2") 'shows 2 decimals after . eg. 3.56 // N4 shows 4 decimals
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Label1.Text = " / "
        End Sub
    End Class

    You need:

    • 2 Textboxes
    • 3 Labels
    • 1 Button

    Copy my code to a new application an test is.
    For example try 5 in first Textbox an 6 in second one.

    Label2 will show 0.833333333 (the complete result)

    Label 3 will show 1 (mathematical rounded and formated as single number.
    In case you meant something else don't hesitate asking again.


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 and VB 2010 Express Be a good forum member - mark posts that contain the answers to your questions or those that are helpful. c# in vb Translator: http://www.developerfusion.com/tools/convert/csharp-to-vb/



    • Edited by Giftzwockel Tuesday, April 17, 2012 9:30 AM
    • Marked as answer by PyrusBoy Tuesday, April 17, 2012 9:31 AM
    Tuesday, April 17, 2012 9:25 AM
  • Thanks, you pointed out my error.

    PyrusBoy

    Tuesday, April 17, 2012 9:32 AM