I was exploring a simple reverse sales-tax calculation in Wolfram Language while working on a small calculator project.
For a final price of $108 with an 8% sales tax:
108/(1 + 8/100)
gives:
100
So the pre-tax price is $100.
I then generalized the calculation as:
reverseTax[price_, rate_] := price/(1 + rate/100)
I’m curious how other Wolfram Language users would approach this. Would you use Manipulate to turn it into a small interactive calculator, or is there a more idiomatic way to structure this?
I’m also wondering how you would handle things like rounding the result to two decimal places and preventing invalid inputs, such as a negative tax rate. Would you handle that validation inside DynamicModule/Manipulate, or separately?
For anyone interested in the practical application, I also put together a simple online version of the reverse-tax calculation at https://calculatereversetax.com.
Here is a little attempt using InpurField:
DynamicModule[{price = 108, rate = 8},
Column[{
Row[{"price = ",
InputField[Dynamic[price]]}],
Row[{"rate = ",
InputField[Dynamic[rate]]}],
Row[{"reverse = ",
Dynamic[price/(1 + rate/100) // N]}]}]]
That’s a nice approach — I like how simple the DynamicModule keeps it.
I was initially thinking about using Manipulate, but InputField actually makes more sense here if the goal is to let the user enter arbitrary prices and tax rates.
One thing I’m still wondering about is input validation. For example, would you normally use NumericQ together with conditions to prevent cases such as a negative rate or a rate of -100%, where the denominator becomes zero?
Also, for a calculator intended for everyday use, would you recommend applying Round[…, 0.01] directly to the result, or keeping the full precision internally and only rounding the displayed value?
Curious how others here would structure this.
Here is a variation:
DynamicModule[{price = 108, rate = 8},
Column[{
Row[{"price = ",
InputField[Dynamic[price], Number]}],
Row[{"rate = ",
InputField[Dynamic[rate], Number]}],
Row[{"reverse = ",
Dynamic[If[rate != -100,
Round[price/(1 + rate/100),
1/100] // N,
$Failed]]}]}]]
For a standalone app you may add the option SaveDefinitions -> True.