Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

TVatCalculator Value Added Tax Calculator Implementation in Delphi

Title: TVatCalculator - Value Added Tax Calculator Implementation in Delphi VAT or Value Added Tax is a common form of taxation in the European Union (EU). VAT is added on top of the cost of a product or service and generates revenue for a government. Here's a small example: if VAT % is 23% and Net value is 200, the Gross value is 246. Value Added Tax Calculator If you are working on an accounting-type application, you will have to deal with VAT, Gross and Net values all the time. The TVATCalculator class provides functions to help you calculate gross from net for a given vat value, net from gross, net for vat, etc... Download TVatCalculator Source Code + Demo Project type TVATCalculator = class private fVatRate: double; public property VatRate : double read fVatRate; constructor Create (const vatValue : double) ; function Vat(const netValue : double) : double; function Gross(const netValue : double) : double; function Net(const grossValue : double) : double; function VatFromGross(const grossValue : double) : double; function NetFromVat(const vatValue : double) : double; function GrossFromVat(const vatValue : double) : double; end; implementation constructor TVATCalculator.Create(const vatValue: double) ; begin fVatRate := vatValue; end; function TVATCalculator.Gross(const netValue: double): double; begin result := netValue + Vat(netValue) ; end; function TVATCalculator.GrossFromVat(const vatValue: double): double; begin result := vatValue + NetFromVat(vatValue) ; end; function TVATCalculator.Net(const grossValue: double): double; begin result := 100 / (100 + VatRate) * grossValue; end; function TVATCalculator.NetFromVat(const vatValue: double): double; begin result := vatValue * 100 / VatRate; end; function TVATCalculator.Vat(const netValue: double): double; begin result := VatRate * netValue / 100 ; end; function TVATCalculator.VatFromGross(const grossValue: double): double; begin result := grossValue - Net(grossValue) ; end; Here's a simple usage example: var vc : TVATCalculator; valueIn : double; begin valueIn := StrToFloat(edValue.Text) ; vc := TVATCalculator.Create(StrToFloat(edVat.Text)) ; try ledVat.Text := FormatFloat(',.00',vc.Vat(valueIn)) ; ledGross.Text := FormatFloat(',.00',vc.Gross(valueIn)) ; ledNet.Text := FormatFloat(',.00',vc.Net(valueIn)) ; ledVatFromGross.Text := FormatFloat(',.00',vc.VatFromGross(valueIn)) ; ledNetFromVat.Text := FormatFloat(',.00',vc.NetFromVat(valueIn)) ; ledGrossFromVat.Text := FormatFloat(',.00',vc.GrossFromVat(valueIn)) ; finally vc.Free; end; end; FormatFloat is used to format a floating point value to a string. Rounding Issues? Now, since in most cases the VAT is calculated for a currency value, that is again, in most cases, presented as a two decimal places value, rounding should be taken into account when VAT calculations are in place. Further more, there are some known bugs with the RoundTo and SimpleRoundTo RTL functions, you might want to consider using DecimalRounding (JH1) by John Herbster.