I am writing a GST calculator as an exercise in Python. At the moment my calculator is hard wired for a rate of 15% and I have the following line for calculating the GST from the GROSS amount:
gst = (gross_amount * 3)/23
However, as GST rates can change, I want to modify this formula so that the only inputs are gross_amount and gst_rate as a percentage, eg 15%
I know this isn’t directly a Python problem but is a maths problem. I have Googled around but everything trots out the same old information, assuming that the rate is a constant.
Many thanks in anticipation.
Assuming that you store your gst_rate as 15 (instead of 0.15):
gst = gross_amount * gst_rate / (gst_rate + 100)
if you store it as 0.15:
gst = gross_amount * gst_rate / (gst_rate + 1)
By golly! That works! I didn’t think it would be that simple. Thank you very much. It’s been quite a while since I did high school maths.
If you have an idea what to do but the stock-in-trade has become rusty, www.wolframalpha.com can do the maths for you. Among many things it’s also great in simplifying boolean expressions.
A gross amount includes the tax so it’s (100 + tax rate)% of the net amount. So it’s all about using the rule of three to get the net amount and calculate the tax from that. Then simplify the formula to make it a little more handy.
Thanks Sam. I’ll bookmark that Wolframalpha site. The IRD recommended formula recommended was multiply by 3 and divide by 23. I noted your ratio matched exactly but, as usual, they view the rate as a constant, not as a variable. So now I guess I can be proud enough to share my code. I also had a bit of trouble calling a method from another method. I worked it out but am still getting my head around this “self” thing.
# Rate defaults to 15%, anything else added as a second arguement
def __init__(self, amount, rate_percent=15):
# Converting amount to cents and using integers for more accurate calculations
self.amount = amount * 100
self.rate_percent = rate_percent
# Adds GST to the amount returning a GROSS amount
gross = int(self.amount * ((self.rate_percent / 100) + 1))
# Removes the GST and returns a NETT amount
nett = int(self.amount - Money.gst(self)*100)
# Returns the GST if the amount is a GROSS amount
gst = int(self.amount * (self.rate_percent / (self.rate_percent + 100)))
m = Money(115)
n = Money(112.5, 12.5)
print(“GST Added”, m.add_gst())
print(“GST Removed”, m.less_gst())
print(“GST Portion”, m.gst())
print(“GST Added”, n.add_gst())
print(“GST Removed”, n.less_gst())
print(“GST Portion”, n.gst())