Please refer to TemplateMonster Support for any issues surrounding TemplateMonster/TemplateTuning Themes – thanks, Jigoshop Team

Tax Conversion

info

22 Jun, 2011 10:23 PM

Using canada tax at 15% I need to type in 150.000 for it to calculate the correct amount. Anything I can do to change that to be 15.0000, not triple digit?

  1. Support Staff 2 Posted by Jigoshop on 23 Jun, 2011 09:54 AM

    Jigoshop's Avatar

    Have you actually confirmed any rounding errors? We forced 4 decimals because I believe thats for the UK (not that I can see any real difference).

  2. 3 Posted by info on 23 Jun, 2011 08:21 PM

    That's what I find interesting. There aren't any errors. What I find curious is the calculation itself.

    Take the base equation for tax:

    Price x Tax = Total Tax Amount to Add

    So, in my case, it would be:

    100 x 0.15 = 15

    With Jigoshop it actually becomes:
    (150.0000 / 100) x 10 = 15

    It doesn't matter what the math is that you are using when it comes to rounding numbers. It's the end result that counts. The end result is what is being inserted to the database.

    In fact, you can also double up on rounding your number by using PHP as well as SQL:

    PHP
    round(1.95583, 2) <--- accepts number, decimal places

    MySQL
    SELECT ROUND(14.537,2) <--- accepts number, decimal places

    or, truncating the decimals past 2 places

    SELECT TRUNCATE(14.537,2) <--- accepts number, decimal places


    The final result of being able to use the basic equation is more accuracy on tax. The equation in use by Jigoshop won't be as accurate for fine tuning tax amounts as well as any reporting services made to the database.

  3. 4 Posted by info on 23 Jun, 2011 08:25 PM

    For quick reference for your tax class:

    /**
     * Calculate the tax using the final value
     *
     * @param   int     Price
     * @param   int     Taxation Rate
     * @return  int     
     */
    function calc_tax( $price, $rate, $price_includes_tax = true ) {
        // To avoid float roudning errors, work with integers (pence)
        $price = round($price * 100, 0);
        $math_rate = $rate;
        if ($price_includes_tax) :
            $math_rate = ($math_rate / 100) + 1;
            $price_ex = round($price / $math_rate);
            $tax_amount = round($price - $price_ex);
        else :
            $tax_amount = $price * ($rate/100);
        endif;
        $tax_amount = $tax_amount / 100; // Back to pounds
        return number_format($tax_amount, 2, '.', '');
    }
    /**
     * Calculate the shipping tax using the final value
     *
     * @param   int     Price
     * @param   int     Taxation Rate
     * @return  int     
     */
    function calc_shipping_tax( $price, $rate ) {
        $rate = round($rate, 4);
        $tax_amount = $price * ($rate/100);
        return round($tax_amount, 2);
    }
    
  4. Support Staff 5 Posted by Jigoshop on 24 Jun, 2011 08:09 AM

    Jigoshop's Avatar

    The tax calc we're using took quite a bit of trial and error because of rounding errors (PHP is terrible with float rounding). Many times we found that (when having price inclusive of tax) we were 1p short.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.