dcsimg
Results 1 to 9 of 9
  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    273

    Resolved [RESOLVED] Roundup value shows subtract sign for the same values ...

    Why the roundup value shows a subtract "-"sign before one invoice calculation for 0.50 value while in another invoice it doesn't? (As in the screenshot of two invoices). The second invoice shows the correct roundup value without substract sign.

    Code:
    Dim TotalAmountWithTax As Double
            TotalAmountWithTax = TotalAmountBeforeTax + TotalCGSTAmount + TotalSGSTAmount
            txtTotalAmountAfterTax.Text = TotalAmountWithTax.ToString("n2")
    
            Dim var1 As Decimal = TotalAmountWithTax 'Assign original value here.
            Dim var2 As Decimal = Math.Round(var1)
            Dim var3 As Decimal = var2 - var1
    
            FRoundOff = var3
            txtRoundOff.Text = FRoundOff.ToString("n2")
    
            If FRoundOff <= 0.5 Then
                FRoundUp = TotalAmountWithTax + FRoundOff
            Else
                FRoundUp = TotalAmountWithTax - FRoundOff
            End If
    
            txtTotalInvoiceAmount.Text = FRoundUp.ToString("n2")
    
            txtTotalInvoiceAmount.Text = Math.Round(TotalAmountWithTax, MidpointRounding.AwayFromZero).ToString("n2")
    Attached Images Attached Images  
    Last edited by VS2013; Feb 10th, 2018 at 03:14 PM.

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

    Re: Roundup value shows subtract sign for the same values ...

    Because you are using two different rounding commands with the same values.

    Code:
    Dim var2 As Decimal = Math.Round(var1)
    When var1 contains a number with a decimal value of exactly .5, this will NOT always round up. It will round to the nearest even integer. So when passed 892.5, the result will be 892 and not 893. When passed 3517.5, the result will be 3518.

    But then below, you are doing:

    Code:
    txtTotalInvoiceAmount.Text = Math.Round(TotalAmountWithTax, MidpointRounding.AwayFromZero).ToString("n2")
    With this Math.Round command, a number with a decimal value of exactly .5 will always round up to the next largest integer.

    So, assuming you always want a number with a decimal value of exactly .5 to be rounded up to the next largest integer, you need to change your var2 declaration:

    Code:
    Dim var2 As Decimal = Math.Round(var1, MidpointRounding.AwayFromZero)
    Last edited by OptionBase1; Feb 10th, 2018 at 03:21 PM.

  3. #3
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    335

    Re: Roundup value shows subtract sign for the same values ...

    Also, based on a quick run-through of the code, I don't think any of the bolded code below is necessary. Certainly not the last bolded line below, since the text in the textbox is immediately replaced with the line of code below it.

    Code:
    Dim TotalAmountWithTax As Double
            TotalAmountWithTax = TotalAmountBeforeTax + TotalCGSTAmount + TotalSGSTAmount
            txtTotalAmountAfterTax.Text = TotalAmountWithTax.ToString("n2")
    
            Dim var1 As Decimal = TotalAmountWithTax 'Assign original value here.
            Dim var2 As Decimal = Math.Round(var1)
            Dim var3 As Decimal = var2 - var1
    
            FRoundOff = var3
            txtRoundOff.Text = FRoundOff.ToString("n2")
    
            If FRoundOff <= 0.5 Then
                FRoundUp = TotalAmountWithTax + FRoundOff
            Else
                FRoundUp = TotalAmountWithTax - FRoundOff
            End If
    
            txtTotalInvoiceAmount.Text = FRoundUp.ToString("n2")
    
            txtTotalInvoiceAmount.Text = Math.Round(TotalAmountWithTax, MidpointRounding.AwayFromZero).ToString("n2")

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,846

    Re: Roundup value shows subtract sign for the same values ...

    This is a lot of "wasted code":
    Code:
            Dim var1 As Decimal = TotalAmountWithTax 'Assign original value here.
            Dim var2 As Decimal = Math.Round(var1)
            Dim var3 As Decimal = var2 - var1
    
            FRoundOff = var3
    Several things in there just make it harder to read, here is a simplified version:
    Code:
            Dim var1 As Decimal = TotalAmountWithTax 'Assign original value here.
            Dim var2 As Decimal = Math.Round(var1)
    
            FRoundOff = var2 - var1
    ...and more so:
    Code:
           FRoundOff = Math.Round(TotalAmountWithTax) - TotalAmountWithTax

    As to the question, you are using Math.Round... which way does it round?

    In some cases it will be up, and in some cases it will be down (and your pictures show one of each).

    You need to decide what behaviour you want, and then use an appropriate method of rounding for that situation (the options include .Floor, .Ceiling, .Round, and many more).


    edit: I should have refreshed the page before posting!

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    273

    Re: Roundup value shows subtract sign for the same values ...

    Thanks for the reply. If the decimal values of TotalAmountWithTax are from .01 to .49 then it should substract the values after decimal. If the decimal values are from .50 to .99 it should add.

    Like:
    150.01 should be 150.00
    ....
    ....
    ....
    150.49 should be 150.00

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

    150.50 should be 151.00
    ...
    ...
    ...
    150.99 should be 151.00

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,846

    Re: Roundup value shows subtract sign for the same values ...

    If you look at the help for Math.Round() with only one parameter: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    ...you will see it says "If the fractional component of d is halfway between two integers, one of which is even and the other odd, the even number is returned." (as that is not what you want, this is not what you should be using)

    If you look at the help for Math.Round (Decimal, MidpointRounding): https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    ...you will see that you have the option of what to do, and if you click on the System.MidpointRounding link you see more information: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    That last link tells you the options available, and gives several examples of how they work... after reading that, I recommend re-reading OptionBase1's posts.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    273

    Re: Roundup value shows subtract sign for the same values ...

    As suggested, I removed the following code, but the result is same without any change. It shows the minus sign before 0.50.
    Like: -0.50

    Code:
    If FRoundOff <= 0.5 Then
                FRoundUp = TotalAmountWithTax + FRoundOff
            Else
                FRoundUp = TotalAmountWithTax - FRoundOff
            End If
    
            txtTotalInvoiceAmount.Text = FRoundUp.ToString("n2")

  8. #8
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    335

    Re: Roundup value shows subtract sign for the same values ...

    Change:

    Code:
    Dim var2 As Decimal = Math.Round(var1)
    to:

    Code:
    Dim var2 As Decimal = Math.Round(var1, MidpointRounding.AwayFromZero)

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    273

    Re: Roundup value shows subtract sign for the same values ...

    Thank you so much. It's working fine.

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


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.