├── composer.json
├── LICENSE
├── src
└── PercentageCalculator.php
└── README.md
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ramazancetinkaya/percentage-calculator",
3 | "description": "A PHP library for performing various percentage calculations",
4 | "type": "library",
5 | "license": "MIT",
6 | "authors": [
7 | {
8 | "name": "Ramazan Çetinkaya",
9 | "email": "ramazancetinkayadev@outlook.com",
10 | "homepage": "https://github.com/ramazancetinkaya"
11 | }
12 | ],
13 | "require": {
14 | "php": ">=8.0"
15 | },
16 | "autoload": {
17 | "psr-4": {
18 | "ramazancetinkaya\\": "src/"
19 | }
20 | },
21 | "minimum-stability": "stable"
22 | }
23 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2024 Ramazan Çetinkaya
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/src/PercentageCalculator.php:
--------------------------------------------------------------------------------
1 |
11 | * @license MIT License (https://opensource.org/licenses/MIT)
12 | * @version 1.0.0
13 | * @link https://github.com/ramazancetinkaya/percentage-calculator
14 | */
15 |
16 | namespace ramazancetinkaya;
17 |
18 | class PercentageCalculator {
19 | /**
20 | * Calculate percentage of a number.
21 | *
22 | * @param float $number The number to calculate the percentage of.
23 | * @param float $percentage The percentage value.
24 | * @return float The result of the percentage calculation.
25 | */
26 | public function calculatePercentage(float $number, float $percentage): float {
27 | $this->validateNumber($number);
28 | $this->validatePercentage($percentage);
29 |
30 | return ($number * $percentage) / 100.0;
31 | }
32 |
33 | /**
34 | * Calculate the percentage change between two numbers.
35 | *
36 | * @param float $originalNumber The original number.
37 | * @param float $newNumber The new number.
38 | * @return float The percentage change between the two numbers.
39 | */
40 | public function calculatePercentageChange(float $originalNumber, float $newNumber): float {
41 | $this->validateNumber($originalNumber);
42 | $this->validateNumber($newNumber);
43 |
44 | if ($originalNumber === 0) {
45 | throw new InvalidArgumentException("Original number cannot be zero.");
46 | }
47 |
48 | return (($newNumber - $originalNumber) / abs($originalNumber)) * 100.0;
49 | }
50 |
51 | /**
52 | * Increase a number by a percentage.
53 | *
54 | * @param float $number The number to increase.
55 | * @param float $percentage The percentage to increase by.
56 | * @return float The increased number.
57 | */
58 | public function increaseByPercentage(float $number, float $percentage): float {
59 | $this->validateNumber($number);
60 | $this->validatePercentage($percentage);
61 |
62 | return $number + ($number * $percentage / 100.0);
63 | }
64 |
65 | /**
66 | * Decrease a number by a percentage.
67 | *
68 | * @param float $number The number to decrease.
69 | * @param float $percentage The percentage to decrease by.
70 | * @return float The decreased number.
71 | */
72 | public function decreaseByPercentage(float $number, float $percentage): float {
73 | $this->validateNumber($number);
74 | $this->validatePercentage($percentage);
75 |
76 | return $number - ($number * $percentage / 100.0);
77 | }
78 |
79 | /**
80 | * Validate the number input.
81 | *
82 | * @param float $number The number to validate.
83 | * @return void
84 | * @throws InvalidArgumentException When the number is not a valid float.
85 | */
86 | private function validateNumber(float $number): void {
87 | if (!is_numeric($number)) {
88 | throw new InvalidArgumentException("Invalid number: $number");
89 | }
90 | }
91 |
92 | /**
93 | * Validate the percentage input.
94 | *
95 | * @param float $percentage The percentage to validate.
96 | * @return void
97 | * @throws InvalidArgumentException When the percentage is not a valid float or not in the range of 0 to 100.
98 | */
99 | private function validatePercentage(float $percentage): void {
100 | if (!is_numeric($percentage) || $percentage < 0 || $percentage > 100) {
101 | throw new InvalidArgumentException("Invalid percentage: $percentage. Percentage must be between 0 and 100.");
102 | }
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Percentage Calculator Library
2 |
3 | [](LICENSE)
4 | [](https://github.com/ramazancetinkaya/percentage-calculator/releases)
5 | 
6 | [](https://github.com/ramazancetinkaya/percentage-calculator/issues)
7 | [](https://github.com/ramazancetinkaya/percentage-calculator/network)
8 | [](https://github.com/ramazancetinkaya/percentage-calculator/stargazers)
9 |
10 | A powerful and modern PHP library for performing various percentage calculations.
11 |
12 | Report a Bug
13 | ·
14 | New Pull Request
15 |
16 | ## 🌟 Star this Repository!
17 |
18 | If you find the Percentage Calculator library helpful or interesting, consider giving it a star! ⭐️
19 |
20 | Your star helps us grow and motivates us to continue improving the library. It also makes it easier for others to discover and benefit from this project.
21 |
22 | ### How to Star?
23 |
24 | 1. **Login to Your GitHub Account:** You need to have a GitHub account.
25 | 2. **Visit the Repository:** Go to the [Percentage Calculator Repository](https://github.com/ramazancetinkaya/percentage-calculator).
26 | 3. **Click the Star Button:** On the top-right corner of the page, you'll find a "Star" button. Click on it!
27 |
28 | That's it! Thank you for your support! 🚀
29 |
30 | ## Table of Contents
31 |
32 | - [Features](#features)
33 | - [Installation](#installation)
34 | - [Usage](#usage)
35 | - [Examples](#examples)
36 | - [Contributing](#contributing)
37 | - [Credits](#credits)
38 | - [License](#license)
39 |
40 | ## Features
41 |
42 | - Calculate the percentage of a number.
43 | - Calculate the percentage change between two numbers.
44 | - Increase a number by a percentage.
45 | - Decrease a number by a percentage.
46 |
47 | ## Installation
48 |
49 | You can install this library via [Composer](https://getcomposer.org/). Run the following command:
50 |
51 | ```bash
52 | composer require ramazancetinkaya/percentage-calculator
53 | ```
54 |
55 | ## Usage
56 |
57 | ```php
58 | require_once 'vendor/autoload.php';
59 |
60 | use ramazancetinkaya\PercentageCalculator;
61 |
62 | $calculator = new PercentageCalculator();
63 | ```
64 |
65 | ## Examples
66 |
67 | ```php
68 | try {
69 | // Calculate percentage
70 | $result = $calculator->calculatePercentage(250, 20);
71 | echo "20% of 250 is: " . $result . "\n";
72 |
73 | // Calculate percentage change
74 | $change = $calculator->calculatePercentageChange(100, 75);
75 | echo "Percentage change from 100 to 75 is: " . $change . "%\n";
76 |
77 | // Increase by percentage
78 | $increase = $calculator->increaseByPercentage(50, 25);
79 | echo "50 increased by 25% is: " . $increase . "\n";
80 |
81 | // Decrease by percentage
82 | $decrease = $calculator->decreaseByPercentage(80, 10);
83 | echo "80 decreased by 10% is: " . $decrease . "\n";
84 | } catch (InvalidArgumentException $e) {
85 | echo "Error: " . $e->getMessage();
86 | }
87 | ```
88 |
89 | ## Contributing
90 |
91 | Contributions are welcome! Please fork the repository and create a pull request.
92 |
93 | ## Credits
94 |
95 | This library was made possible by the following awesome contributors:
96 |
97 | - **Ramazan Çetinkaya** - [@ramazancetinkaya](https://github.com/ramazancetinkaya)
98 | - Lead Developer
99 |
100 | Special thanks to the following resources:
101 |
102 | - [PHP Documentation](https://www.php.net/docs.php) - Valuable information on PHP programming language.
103 | - [Composer](https://getcomposer.org/) - Dependency manager for PHP.
104 |
105 | If you've contributed to this project and your name is not listed, please let us know, and we'll add you!
106 |
107 | Thank you to everyone who has helped make this project better!
108 |
109 | ## License
110 |
111 | This project is licensed under the MIT License. For more details, see the [LICENSE](LICENSE) file.
112 |
--------------------------------------------------------------------------------