Home > OS >  How do I get this mathematical rounding?
How do I get this mathematical rounding?

Time:11-05

I am trying to replicate a math formula in VB.NET in order to achieve the same sum as a government financial tool that I must stay conform with.

The official tool's result and mine differ by 0.1 €, and I don't see what I need to do to achieve the same result.

The official tool states:

A product costs 95.00 € net.
Somebody buys 3 pieces of it.
He gets a discount of 5 % (=4.75 €)
The net price is then 90.25 €
The price is now 270.75 €
19% VAT is added to this amount.
The resulting end price is now 322.19 €.

The values that I am given are these:

net price for 1 piece: 95 €
amount: 3
discount per piece: 5%
VAT: 17.15 

First, I calculate the discount like this:

Dim amount As Integer = 3
Dim discount As Double = 5
Dim VAT As Double = 17.15
All result As Decimal 

Discount = (amount * discount) / 100 = 4.75
Discounted net price per piece = 95 - 4.75 = 90.25
Discounted net price per piece plus vat = 95   17.15 = 107.4

Total price result = discounted net price per piece * amount = 107.4 * 3 = 322.2 €

Is there anything that I'm doing wrong by choosing a wrong operator?

Is it even possible to get the same amount as they do with the information that I am given?

Thank you.

CodePudding user response:

EDIT:

I just realised that you actually did use the correct discounted net price in the calculation below but you just wrote that you didn't, i.e. the result is correct even if the operands aren't.

ORIGINAL:

You do this:

Discounted net price per piece = 95 - 4.75 = 90.25

but then here:

Discounted net price per piece plus vat = 95 17.15 = 107.4

you don't use the result of that previous calculation.

Also, even if you did use the discounted price, how does it make sense to add a percentage to a price? Surely you need to calculate that percentage of that price, then add that result to that price. I would think that that line should be this:

Discounted net price per piece plus vat = 90.25 (90.25 * 17.15 / 100) = 105.7(27875)

CodePudding user response:

According to enter image description here

Private Sub Calculate()
    Dim DiscountAmount As Decimal = 0
    Dim DiscountPercentage As Decimal = 0
    Dim NetPrice As Decimal = 0
    Dim Price As Decimal = 0
    Dim Quantity As Decimal = 0
    Dim VATAmount As Decimal = 0
    Dim VATPercentage As Decimal = 0
    Dim GrossSalesPrice As Decimal = 0

    'ToDo: Ensure that the user entered valid input. If not, notify the user of invalid input

    'convert String to Decimal
    Decimal.TryParse(TextBoxPrice.Text, Price)
    Decimal.TryParse(TextBoxDiscountPercentage.Text, DiscountPercentage)
    Decimal.TryParse(TextBoxVATPercentage.Text, VATPercentage)
    Decimal.TryParse(TextBoxQuantity.Text, Quantity)

    Dim currencySymbol As String = System.Globalization.CultureInfo.GetCultureInfo("de-DE").NumberFormat.CurrencySymbol

    Debug.WriteLine($"A product costs {Price.ToString("N2")} {currencySymbol} net.")
    Debug.WriteLine($"Somebody buys {Quantity.ToString("N0")} pieces of it.")

    'calculate discount amount
    DiscountAmount = Decimal.Multiply(Price, Decimal.Divide(DiscountPercentage, 100))
    Debug.WriteLine($"He gets a discount of {DiscountPercentage.ToString("N0")}% (={DiscountAmount.ToString("N2")} {currencySymbol})")

    'calculate net price
    NetPrice = Decimal.Subtract(Price, DiscountAmount)
    Debug.WriteLine($"The net price is then {NetPrice.ToString("N2")} {currencySymbol}")

    'calculate gross price
    Dim NetSalesPrice As Decimal = Decimal.Multiply(NetPrice, Quantity)
    Debug.WriteLine($"The price is now {NetSalesPrice.ToString("N2")} {currencySymbol}")

    'set value
    TextBoxNetSalesPrice.Text = NetSalesPrice.ToString("N2")

    Debug.WriteLine($"{VATPercentage.ToString("N0")}% VAT is added to this amount.")

    'calculate VAT amount
    VATAmount = Decimal.Multiply(NetSalesPrice, Decimal.Divide(VATPercentage, 100))

    'set value
    TextBoxVATAmount.Text = VATAmount.ToString("N2")

    'calculate grand total
    'GrossSalesPrice = Decimal.Multiply(NetTotal, Decimal.Add(1, Decimal.Divide(VATPercentage, 100)))
    GrossSalesPrice = Decimal.Add(NetSalesPrice, VATAmount)

    Debug.WriteLine($"The resulting end price is now {GrossSalesPrice.ToString("N2")} {currencySymbol}")

    'set value
    TextBoxGrossSalesPrice.Text = GrossSalesPrice.ToString("N2")
End Sub

Usage:

Calculate()

Resources:

  • Related