locked
How to round to two decimal places

    Question

  • Hi,

    I have a question about rounding number to two decimal places.

    I have looked around and tried different thigns, but they don't seem to work :\

    I am making a calculator which people use to help them with maths for an assignment at school. All of the modes  such as Multiplication, subtraction and addition work, but I am having troubles with division.

    I have tried using variables such as Integers, which didn't help, and "Dim DivMode (name of the vairable I am using) as Double" which gives me outrageous numbers to about 15 decimal places aswell.

    I had a problem with the Dim as Integer, as it would round as a whole, giving me a question such as 5/2 = 3 when really it would be 2.5

    So far my code is as follows:

     

    If RB_Div.Checked = True Then

    Number = Int(Rnd() * 50) + 1

    TextBox1.Text = Number

    FirstNum = Number

    Number = Int(Rnd() * 25) + 1

    TextBox2.Text = Number

    SecondNum = Number

    DivMode = FirstNum / SecondNum

    lbl_Answer.Text = DivMode

     

    End If

    That's the code, which has also made my text smaller here >.< Sorry about that.

    Anyway to fic that up so I can get number so bigger than two decimal places such as 2.25?

    the "DivMode" has been declared as a Double.

    This is due on Friday, so help asap would be greatly appreciated :)\

    Btw: I am a bit of a noob using this program. I know the basics, but if you have an answer, please explain it well xD Or show me some code i can use :)

    Tuesday, November 16, 2010 11:12 PM

Answers

  • Hi Fyro,

    You can also use the Format code. Here a little example:

    Public Class Form1

    Public Round As Double

    Private Sub TextBox1_TextChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    Me .Label1.Text = Round

    Round = Me .TextBox1.Text

    End Sub

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

    Me .TextBox1.Text = Format(Round, "0.00" )

    End Sub

    End Class

    The key function is:

    Me .TextBox1.Text = Format(Round, "0.00" )

    So if you want 4 demicals instead of only 2 write

    Me .TextBox1.Text = Format(Round, "0.0000" )

    I think, your line will look similar like this:

    lbl_Answer.Text = Format (DivMode, "0,00")



    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by Liliane Teng Tuesday, November 23, 2010 7:23 AM
    Wednesday, November 17, 2010 12:24 PM
  • As you are dividing one integer into another the result will be of type Double.  Therefore DivMode must be declared as a Double.

    In order to round the result simply use the Math.Round function.

    • Marked as answer by Liliane Teng Tuesday, November 23, 2010 7:23 AM
    Tuesday, November 16, 2010 11:36 PM
  •         'Do you want only the answer to be rounded to
            'two decimal places, or the display of the answer
            'to be rounded to two decimal places.  This does both...

            Dim dividend As Double
            Dim divisor As Double
            Dim quotient As Double

            If Double.TryParse(TextBox1.Text, dividend) Then
                'we have a valid number for dividend
                'force two decimal places
                dividend = Math.Round(dividend, 2)
                TextBox1.Text = dividend.ToString("n2")
                If Double.TryParse(TextBox2.Text, divisor) Then
                    'we have a valid number for divisor
                    'force two decimal places
                    divisor = Math.Round(divisor, 2)
                    TextBox2.Text = divisor.ToString("n2")
                    quotient = dividend / divisor
                    'force two decimal places
                    quotient = Math.Round(quotient, 2)
                    Label1.Text = quotient.ToString("n2")
                End If
            End If

    Subnet Calculator / Planner      Serial Port      Random
    • Proposed as answer by Giftzwockel Wednesday, November 17, 2010 12:24 PM
    • Marked as answer by Liliane Teng Tuesday, November 23, 2010 7:23 AM
    Wednesday, November 17, 2010 11:11 AM
  • Fyro,

    Rounding is not as easy as it looks in .Net (every program language). 

    If banking rounding is used, mostly it goes without problems, but most persons use rounding away from Zero. 

    Have a look at this midpointrounding enumeration page where it is very well described.

    http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx

    Be aware that rounding in VB for Net so in Express does not follow the same rules as other versions from VB


    Success
    Cor
    • Marked as answer by Liliane Teng Tuesday, November 23, 2010 7:26 AM
    Thursday, November 18, 2010 9:18 AM
  • Good point Cor.  The programmer should be aware of what rounding is used and the affect.

            Dim d As Double

            For d = -4.45 To 4.45 Step 0.5
                Dim dde As Double = Math.Round(d, 1, MidpointRounding.ToEven)
                Dim ddz As Double = Math.Round(d, 1, MidpointRounding.AwayFromZero)
                Dim mrk As String = If(dde = ddz, "", ">")
                Debug.WriteLine(mrk & d.ToString("n2") & " " & dde.ToString("n2") & " " & ddz.ToString("n2"))
            Next


    Subnet Calculator / Planner      Serial Port      Random
    • Marked as answer by Liliane Teng Tuesday, November 23, 2010 7:26 AM
    Thursday, November 18, 2010 12:23 PM

All replies

  • As you are dividing one integer into another the result will be of type Double.  Therefore DivMode must be declared as a Double.

    In order to round the result simply use the Math.Round function.

    • Marked as answer by Liliane Teng Tuesday, November 23, 2010 7:23 AM
    Tuesday, November 16, 2010 11:36 PM
  •         'Do you want only the answer to be rounded to
            'two decimal places, or the display of the answer
            'to be rounded to two decimal places.  This does both...

            Dim dividend As Double
            Dim divisor As Double
            Dim quotient As Double

            If Double.TryParse(TextBox1.Text, dividend) Then
                'we have a valid number for dividend
                'force two decimal places
                dividend = Math.Round(dividend, 2)
                TextBox1.Text = dividend.ToString("n2")
                If Double.TryParse(TextBox2.Text, divisor) Then
                    'we have a valid number for divisor
                    'force two decimal places
                    divisor = Math.Round(divisor, 2)
                    TextBox2.Text = divisor.ToString("n2")
                    quotient = dividend / divisor
                    'force two decimal places
                    quotient = Math.Round(quotient, 2)
                    Label1.Text = quotient.ToString("n2")
                End If
            End If

    Subnet Calculator / Planner      Serial Port      Random
    • Proposed as answer by Giftzwockel Wednesday, November 17, 2010 12:24 PM
    • Marked as answer by Liliane Teng Tuesday, November 23, 2010 7:23 AM
    Wednesday, November 17, 2010 11:11 AM
  • Hi Fyro,

    You can also use the Format code. Here a little example:

    Public Class Form1

    Public Round As Double

    Private Sub TextBox1_TextChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    Me .Label1.Text = Round

    Round = Me .TextBox1.Text

    End Sub

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

    Me .TextBox1.Text = Format(Round, "0.00" )

    End Sub

    End Class

    The key function is:

    Me .TextBox1.Text = Format(Round, "0.00" )

    So if you want 4 demicals instead of only 2 write

    Me .TextBox1.Text = Format(Round, "0.0000" )

    I think, your line will look similar like this:

    lbl_Answer.Text = Format (DivMode, "0,00")



    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 Express
    • Marked as answer by Liliane Teng Tuesday, November 23, 2010 7:23 AM
    Wednesday, November 17, 2010 12:24 PM
  • Fyro,

    Rounding is not as easy as it looks in .Net (every program language). 

    If banking rounding is used, mostly it goes without problems, but most persons use rounding away from Zero. 

    Have a look at this midpointrounding enumeration page where it is very well described.

    http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx

    Be aware that rounding in VB for Net so in Express does not follow the same rules as other versions from VB


    Success
    Cor
    • Marked as answer by Liliane Teng Tuesday, November 23, 2010 7:26 AM
    Thursday, November 18, 2010 9:18 AM
  • Good point Cor.  The programmer should be aware of what rounding is used and the affect.

            Dim d As Double

            For d = -4.45 To 4.45 Step 0.5
                Dim dde As Double = Math.Round(d, 1, MidpointRounding.ToEven)
                Dim ddz As Double = Math.Round(d, 1, MidpointRounding.AwayFromZero)
                Dim mrk As String = If(dde = ddz, "", ">")
                Debug.WriteLine(mrk & d.ToString("n2") & " " & dde.ToString("n2") & " " & ddz.ToString("n2"))
            Next


    Subnet Calculator / Planner      Serial Port      Random
    • Marked as answer by Liliane Teng Tuesday, November 23, 2010 7:26 AM
    Thursday, November 18, 2010 12:23 PM