A small CoffeeScript/JavaScript function and PHP class for calculating percentages that solves rounding issues.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
example
source/coffee
tests
.gitignore
Gruntfile.js
README.md
bower.json
package.json

README.md

Percentages

Bower Version

A small CoffeeScript / JavaScript function and PHP class for calculating percentages that solves rounding issues.

How does it work

Both Percentages.js and Percentages.class.php use the principle of the largest remainder method. Each percentage will be rounded down at first. After that, the script will add 1 percent to a rounded value if this value has the highest remainder. This process continues until a total of 100% is reached.

The function and class both return (1) absolute percentages, (2) rounded percentages and (3) the corrected percentages.

Note: because the scripts loop through remainders until a total of 100% is reached, a difference of 1% can occur between two of the same values. See the last example below.

Supported browsers (for JS)

Chrome, Firefox, Safari 4+, IE9+

Installation

Install with Bower

$ bower install percentages

Or: include files

Include the CoffeeScript, JavaScript or PHP file.

Examples

JavaScript example

// For comparison, the function returns all 3 types of percentages
var votes = [12, 30, 2, 7, 15],
    percentages = new Percentages(votes);

// Most likely, you only want to use the corrected percentages
// Use this instead:
var votes = [12, 30, 2, 7, 15],
    percentage = new Percentages(votes).corrected;

PHP example

// By default all 3 types will be returned
$votes = array(12, 30, 2, 7, 15);
$percentages = new Percentages();
$percentages = $percentages->get();

// If you only would like a specific type, use a parameter in the get() method
// Note that only "abs", "rounded" and "corrected" are valid
$percentages = $percentages->get("corrected");

Rounding issue example

As said above: because the scripts loop through remainders until a total of 100% is reached, a difference of 1% can occur between two of the same values.

$votes = array(1, 1, 1, 1, 1, 1);
$percentages = new Percentages();
$percentages = $percentages->get("corrected"); // output: array(17, 17, 17, 17, 16, 16)

Testing

Run automated tests with command line.

JavaScript

This requires grunt-contrib-jasmine, which is installed as a devDependency.

$ grunt test

PHP

This requires PHPUnit.

$ cd tests
$ phpunit