locked
Rounding to 2 decimal places

    Question

  • I've just installed Visual Basic 2008 express.  and suddenly hit the problem of rounding. I can round to no decial places but alot of my work requires 2 decimal places.  How is this done?

    cheers 
    • Edited by Angelus200 Thursday, September 25, 2008 5:24 AM
    Thursday, September 25, 2008 5:24 AM

Answers

  • Hi,

    Use

    Math.Round(someNumber ,2)

    E.G.

    Option Strict On

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim pi As Double = Math.PI
    pi = Math.Round(pi, 2)
    MessageBox.Show(pi.ToString)

    End Sub

    End Class




    Regards,

    John


    For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835
    • Marked as answer by Xingwei Hu Tuesday, September 30, 2008 4:34 AM
    Thursday, September 25, 2008 5:58 AM
  • If you just want to display the number rounded, you could do something like this.  This leaves the variable unchanged.

    Dim cNum As Decimal = 123.4567D
    TextBox1.Text = cNum.ToString(
    "0.00")

    • Edited by SMD- Thursday, September 25, 2008 2:48 PM minor mod
    • Marked as answer by Xingwei Hu Tuesday, September 30, 2008 4:34 AM
    Thursday, September 25, 2008 11:52 AM

All replies

  • Hi,

    Use

    Math.Round(someNumber ,2)

    E.G.

    Option Strict On

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim pi As Double = Math.PI
    pi = Math.Round(pi, 2)
    MessageBox.Show(pi.ToString)

    End Sub

    End Class




    Regards,

    John


    For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835
    • Marked as answer by Xingwei Hu Tuesday, September 30, 2008 4:34 AM
    Thursday, September 25, 2008 5:58 AM
  • If you just want to display the number rounded, you could do something like this.  This leaves the variable unchanged.

    Dim cNum As Decimal = 123.4567D
    TextBox1.Text = cNum.ToString(
    "0.00")

    • Edited by SMD- Thursday, September 25, 2008 2:48 PM minor mod
    • Marked as answer by Xingwei Hu Tuesday, September 30, 2008 4:34 AM
    Thursday, September 25, 2008 11:52 AM
  • John Anthony Oliver said:

    Hi,

    Use

    Math.Round(someNumber ,2)

    E.G.

    Option Strict On

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim pi As Double = Math.PI
    pi = Math.Round(pi, 2)
    MessageBox.Show(pi.ToString)

    End Sub

    End Class




    Regards,

    John


    For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835



    Great  Thanks :)
    Wednesday, November 05, 2008 8:38 PM
  •     Private Sub TestRounding() 
            'ceiling - This kind of rounding is sometimes called rounding toward positive infinity. 
            'floor - This kind of rounding is sometimes called rounding toward negative infinity.  
            'round - If the fractional component of d is halfway between two integers, one of which is even and the other odd, then the even number is returned. 
            'ceiling / floor only return integers 
            Dim fD, cD, rD, xD As Decimal 
            Dim ccD As Decimal 
            For xD = -1D To 1D Step 0.1D ' 0.01D 
                fD = Math.Floor(xD) 
                cD = Math.Ceiling(xD) 
                ccD = Math.Ceiling(xD * 10D) / 10D 'ceiling precision one decimal 
                rD = Math.Round(xD, 1) 
                Debug.Write("'" & xD.ToString.PadRight(6, " "c) & " ") 'xD 
                Debug.Write(" Floor " & fD.ToString.PadRight(6, " "c) & " "'fD 
                Debug.Write(" Ceil  " & cD.ToString.PadRight(6, " "c) & " "'cD 
                Debug.Write(" Ceil.x  " & ccD.ToString.PadRight(6, " "c) & " "'cD 
                Debug.WriteLine(" Round " & rD.ToString) 'rD 
            Next 
        End Sub 
     

    Wednesday, November 05, 2008 9:47 PM