RobsonLittle Red Book ▸ Prices

Chapter 1 - Question 2

Given the price, in pounds, of an item, any discount allowed and the applicable VAT rate, find and output the cost of an item.

Solution 1

<?    // the current uk vat rate    define(VAT_RATE, 17.5);        // the price in pounds of an item    // produces a result between 1 and 10    $price = mt_rand(1, 20);    // the discount for this item (percent)    // produces a result between 1 and 20    $discount = mt_rand(1, 20);        // show the original price    // number format is used to make sure the price has two decimal places    echo . number_format($price, 2) . '<br /> <br />';        // work out the discount    $item_discount = $price * ($discount / 100);    // the cost would be the original price minus the discount    $price -= $item_discount;    // show the discount    echo $discount . . number_format($item_discount, 2) . '<br />';    // show the price after discount        echo . number_format($price, 2) . '<br /> <br />';        // work out the vat    $item_vat = $price * (VAT_RATE / 100);    // plus vat    $price += $item_vat;    // show the vat    echo VAT_RATE . . number_format($item_vat, 2) . '<br />';    // show the price after vat has been added    echo . number_format($price, 2); ?>

Which produces:

Original price: 13.00

19% discount: 2.47
After discount: 10.53

17.5% VAT: 1.84
After VAT: 12.37

Log

© 2004-20 robson | cc unless stated