
Feb 10th, 2018, 02:45 PM
#1
Thread Starter
Hyperactive Member
Last edited by VS2013; Feb 10th, 2018 at 03:14 PM.

Feb 10th, 2018, 03:17 PM
#2
Hyperactive Member
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.

Feb 10th, 2018, 03:24 PM
#3
Hyperactive Member
Re: Roundup value shows subtract sign for the same values ...
Also, based on a quick runthrough 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")

Feb 10th, 2018, 03:25 PM
#4
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!

Feb 10th, 2018, 03:51 PM
#5
Thread Starter
Hyperactive Member
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

Feb 10th, 2018, 04:11 PM
#6
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/enus/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/enus/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/enus/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 rereading OptionBase1's posts.

Feb 10th, 2018, 09:40 PM
#7
Thread Starter
Hyperactive Member
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")

Feb 10th, 2018, 09:43 PM
#8
Hyperactive Member
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)

Feb 11th, 2018, 12:38 PM
#9
Thread Starter
Hyperactive Member
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

Forum Rules

Click Here to Expand Forum to Full Width
