/percentage

Clean interface to calculate percentages in Go

Primary LanguageGoMIT LicenseMIT

Clean interface to calculate percentages in Go

Go version Go Report Card Godoc Build Status codecov Releases LICENSE

A clean interface to calculate percentages in Go: changes between values, percentage increases and partial values. Think of it as a very lightweight math library for the basic stuff. Aimed to make your code more readable and easier to understand. Inspired by mattiasgeniar/php-percentages

Installation

You can install this module with go get command:

go get -u github.com/rorecek/percentage

Usage

import "github.com/rorecek/percentage"

// What's the percentage increase from 100 to 120?
percentage.DifferenceBetween(100, 120);         // 20

// What's the absolute percentage change from 100 to 80?
percentage.AbsoluteDifferenceBetween(100, 80);  // 20, not -20

// How much is 120 compared to 100?
percentage.Calculate(120, 100);                 // 120

// How much is 50 compared to 100?
percentage.Calculate(50, 100);                  // 50

// What is 20% of 200?
percentage.Of(20, 200);                         // 40

Testing

go test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email pavel@rorecek.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.