dcsimg
Results 1 to 3 of 3

Thread: Round Value related query ...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    273

    Round Value related query ...

    I want to get round value as below:

    If it is equal to 646.50 then the rounded value should be 647.00 [Here, I am getting the result as 646.00]
    If it is equal to 646.49 then the rounded value should be 646.00

    Note: What I mean is, If the decimal value is less than or equal to .49 then it should deduct 1 from the Invoice value, and if it is equal or greater than .50 then it should add 1 to the Invoice value. How to modify the below code?

    Code:
    Dim var1 As Decimal = InvoiceValue 'Assign original value here.
            Dim var2 As Decimal = Math.Round(var1)
            Dim var3 As Decimal = var2 - var1
    
            Dim FRoundOff As Double
            Dim FRoundUp As Double
    
            FRoundOff = var3
            txtRoundOff.Text = FRoundOff.ToString("n2")
    
            If FRoundOff <= 0.5 Then
                FRoundUp = InvoiceValue + FRoundOff
            Else
                FRoundUp = InvoiceValue - FRoundOff
            End If
    
            txtTotalInvoiceValue.Text = FRoundUp.ToString("n2")

  2. #2
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    372

    Re: Round Value related query ...

    Quote Originally Posted by VS2013 View Post
    I want to get round value as below:

    If it is equal to 646.50 then the rounded value should be 647.00 [Here, I am getting the result as 646.00]
    If it is equal to 646.49 then the rounded value should be 646.00

    Note: What I mean is, If the decimal value is less than or equal to .49 then it should deduct 1 from the Invoice value, and if it is equal or greater than .50 then it should add 1 to the Invoice value. How to modify the below code?

    Code:
    Dim var1 As Decimal = InvoiceValue 'Assign original value here.
            Dim var2 As Decimal = Math.Round(var1)
            Dim var3 As Decimal = var2 - var1
    
            Dim FRoundOff As Double
            Dim FRoundUp As Double
    
            FRoundOff = var3
            txtRoundOff.Text = FRoundOff.ToString("n2")
    
            If FRoundOff <= 0.5 Then
                FRoundUp = InvoiceValue + FRoundOff
            Else
                FRoundUp = InvoiceValue - FRoundOff
            End If
    
            txtTotalInvoiceValue.Text = FRoundUp.ToString("n2")
    I believe Math.Round by default "rounds" to the nearest even integer when the decimal portion is exactly .5

    If all you are looking for is what I consider to be proper rounding, where PositiveNumber.5 always rounds up to (PositiveNumber+1), then do this:

    Code:
    txtTotalInvoiceValue.Text = Math.Round(InvoiceValue, MidpointRounding.AwayFromZero).ToString("n2")

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,678

    Re: Round Value related query ...

    The Math.Round Method has several ways to round depending on it's parameters https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    Just converting a decimal to integer will round the way you want, try this
    Code:
            Dim var As Decimal = CDec(1.4999)
            Dim intVar As Integer = CInt(var)
    
            MessageBox.Show(intVar.ToString)
    Then change var to 1.5 and try again.
    Last edited by wes4dbt; Jan 28th, 2018 at 02:51 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width