Friday, 14 June 2013

Working with percentages

I wouldn't say that I'm the best mathematician; if I'm being generous, my skills in the maths department is somewhere about average which isn't always a good place to be for a programmer. But what I lack here I make up for in understanding of how computers work and how to program them, and this is what acts as a stabiliser against the wrath of BODMAS and such like.

So, I've been working on a project for a client which in part has to work out percentages of a price, like adding VAT levied by the Government on saleable goods. Now, this is actually the easy bit. VAT, at the time of writing, is 20%, so if an item costs £100 without it, that item will be £120 including VAT. So, writing the code for this is simple: take the current price, work out 20% and add that to it. Or, multiply the current price by 1.20, which does the same thing.

Writing the code for this is easy, in a C-like language, it would be something like:

// assuming £80.00p
float price = 80.00f;
// This is a quick way to add VAT @ 20%
float priceWithVAT = price * 1.20f;
// So...
printf( "Price ex-VAT: %.2f", price );
printf( "Price with VAT: %.2f", priceWithVAT );
  

Great! Assuming the VAT rate will always be 20%, this will always work. But not long ago, VAT was at 17.5%, and before that, which dropped temporarily to 15%. Now, let's say that you want to work with different percentage rates, or the rate unexpectedly changes. Something like the following will work:

/**
 * A quick demonstration of working out
 * percentages using C will probably work
 * on all other problem-orientated
 * languages.
 *
 * Written with Code::Blocks, tested on
 * Windows 7 and cmd.exe
 *
 * @Author:  Shaun B
 * @Version: 1.0.1 - 2013-06-14
 * @Todo:    Look into floating point guides and why
 *           there are rounding errors
 */

#include <stdio.h>
#include <stdlib.h>

// Function prototypes:
int main();
float percent (float pc);
float taxToAdd (float number, float percent);

// Variables:
float taxRate = 20.00f; // ie VAT, PAYE etc...
float percentage = 0.00f;
float taxToPay = 0.00f;
float priceWithTax = 0.00f;

int main()
{
    float originalPrice = 59.99f;
    unsigned char pound = 156;

    percentage =
        percent (taxRate);
    taxToPay =
        taxToAdd (originalPrice, percentage);
    priceWithTax =
        originalPrice + taxToPay;

    printf(
        "Tax rate set to: %.2f\%%\n", taxRate
    );
    printf(
        "Original price of goods less tax: %c%.2f\n",
        pound, originalPrice
    );
    printf("Tax on goods: %c%.2f\n", pound, taxToPay);
    printf(
        "Total payable: %c%.2f\n", pound, priceWithTax
    );

    printf("Fin. Press any key.");
    _getch();

    return 0;
}

float percent (float pc)
{
    return (float)pc/100.00f;
}

float taxToAdd (float price, float percent)
{
    return (float)price*percent;
}
  

Things should be fairly self-explanatory here. This example means that if your tax rate changes, you only have to change one variable rather than tracing all of the instances of *1.20f in your source code. This makes it easy to set percentage rates for other taxes, levies or fees that a client may encounter. The only problem now is what to do with the third decimal place, say if something works out to be £17.8555 with fees or taxes added? I think things are usually rounded up though.

Note that the £ sign doesn't appear on my computer here - all of the documentation online says that it should be 0xa3 (or 163 in human), but I found it to be 156 (or 0x9c in hexadecimal) on this Windows 7 machine.

No comments:

Post a Comment