PHPackages                             arraypress/wp-math-utils - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. arraypress/wp-math-utils

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

arraypress/wp-math-utils
========================

A lean WordPress library for mathematical calculations, e-commerce operations, and number utilities

015PHP

Since Oct 3Pushed 7mo agoCompare

[ Source](https://github.com/arraypress/wp-math-utils)[ Packagist](https://packagist.org/packages/arraypress/wp-math-utils)[ RSS](/packages/arraypress-wp-math-utils/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

WordPress Math Utils
====================

[](#wordpress-math-utils)

A lean WordPress library for business calculations and number formatting, focusing on e-commerce and analytics operations.

Installation
------------

[](#installation)

```
composer require arraypress/wp-math-utils
```

Quick Start
-----------

[](#quick-start)

```
use ArrayPress\MathUtils\Math;

E-commerce calculations
$discount = Math::discount(100, 20, true);        20% off: ['discount' => 20, 'final_price' => 80]
$tax = Math::tax(100, 8.25, false);               Add tax: ['tax' => 8.25, 'price_with_tax' => 108.25]

Business metrics
$margin = Math::profit_margin(120, 100);          16.67% profit margin
$change = Math::percentage_change(100, 120);      20% increase

Number formatting
$abbreviated = Math::abbreviate(1500000);         "1.5M"
$ordinal = Math::ordinal(23);                     "23rd"
```

Math Class
----------

[](#math-class)

### `discount(float $price, float $discount, bool $is_percentage = true, int $precision = 2): array`

[](#discountfloat-price-float-discount-bool-is_percentage--true-int-precision--2-array)

Apply discount (percentage or flat amount).

```
Percentage discount
$result = Math::discount(100, 20, true);
Returns: ['discount' => 20.00, 'final_price' => 80.00]

Flat discount
$result = Math::discount(100, 15, false);
Returns: ['discount' => 15.00, 'final_price' => 85.00]
```

### `tax(float $price, float $rate, bool $inclusive = false, int $precision = 2): array`

[](#taxfloat-price-float-rate-bool-inclusive--false-int-precision--2-array)

Calculate tax (inclusive or exclusive).

```
Exclusive tax (add to price)
$result = Math::tax(100, 8.25, false);
Returns: ['tax' => 8.25, 'price_with_tax' => 108.25]

Inclusive tax (extract from price)
$result = Math::tax(108.25, 8.25, true);
Returns: ['tax' => 8.25, 'price_without_tax' => 100.00]
```

### `profit_margin(float $revenue, float $cost, int $precision = 2): float`

[](#profit_marginfloat-revenue-float-cost-int-precision--2-float)

Calculate profit margin as percentage.

```
$margin = Math::profit_margin(120, 100); 16.67% margin
$margin = Math::profit_margin(150, 100); 33.33% margin
```

### `percentage_change(float $old_value, float $new_value, int $precision = 2): float`

[](#percentage_changefloat-old_value-float-new_value-int-precision--2-float)

Calculate percentage change between values.

```
$change = Math::percentage_change(100, 120); 20.00 (20% increase)
$change = Math::percentage_change(120, 100); -16.67 (16.67% decrease)
```

### `conversion_rate(float $conversions, float $total, int $precision = 2): float`

[](#conversion_ratefloat-conversions-float-total-int-precision--2-float)

Calculate conversion rate as percentage.

```
$rate = Math::conversion_rate(25, 1000); 2.5% conversion rate
$rate = Math::conversion_rate(5, 100);   5% conversion rate
```

### `abbreviate(float $number, int $precision = 1): string`

[](#abbreviatefloat-number-int-precision--1-string)

Abbreviate large numbers for display.

```
Math::abbreviate(1500);         "1.5K"
Math::abbreviate(2500000);      "2.5M"
Math::abbreviate(1200000000);   "1.2B"
```

### `ordinal(int $number): string`

[](#ordinalint-number-string)

Get ordinal suffix for numbers.

```
Math::ordinal(1);    "1st"
Math::ordinal(22);   "22nd"
Math::ordinal(103);  "103rd"
```

Real-World Examples
-------------------

[](#real-world-examples)

### E-commerce Checkout

[](#e-commerce-checkout)

```
$item_price = 99.99;

Apply 20% off coupon
$discount_result = Math::discount($item_price, 20, true);
$discounted_price = $discount_result['final_price']; 79.99

Add 8.25% tax
$tax_result = Math::tax($discounted_price, 8.25, false);
$final_total = $tax_result['price_with_tax']; 86.59

echo "Original: $" . number_format($item_price, 2);
echo "Discount: -$" . number_format($discount_result['discount'], 2);
echo "Tax: $" . number_format($tax_result['tax'], 2);
echo "Total: $" . number_format($final_total, 2);
```

### Business Analytics Dashboard

[](#business-analytics-dashboard)

```
Monthly revenue analysis
$last_month = 45000;
$this_month = 52000;

$change = Math::percentage_change($last_month, $this_month);
echo "Revenue: " . Math::abbreviate($this_month); "52K"
echo "Growth: " . number_format($change, 1) . '%'; "15.6%"

Profit margins
$revenue = 120000;
$costs = 85000;
$margin = Math::profit_margin($revenue, $costs);
echo "Profit Margin: " . number_format($margin, 2) . '%'; "29.17%"
```

### Dynamic Pricing Calculator

[](#dynamic-pricing-calculator)

```
Bulk discount tiers
$quantity = 25;
$unit_price = 10.00;
$subtotal = $quantity * $unit_price;

Apply quantity discount
if ($quantity >= 20) {
    $discount = Math::discount($subtotal, 15, true);
    $subtotal = $discount['final_price'];
}

Calculate tax
$with_tax = Math::tax($subtotal, 8.25, false);
$total = $with_tax['price_with_tax'];

echo "Items: " . $quantity;
echo "Subtotal: $" . number_format($subtotal, 2);
echo "Tax: $" . number_format($with_tax['tax'], 2);
echo "Total: $" . number_format($total, 2);
```

### Analytics Display

[](#analytics-display)

```
Format large numbers for dashboard
$views = 1234567;
$sales = 45678;
$conversion = Math::conversion_rate($sales, $views);

echo "Views: " . Math::abbreviate($views);        "1.2M"
echo "Sales: " . Math::abbreviate($sales);        "45.7K"
echo "Conversion: " . number_format($conversion, 2) . '%'; "3.70%"

Ordinal rankings
$position = 3;
echo "You're in " . Math::ordinal($position) . " place!"; "You're in 3rd place!"
```

Requirements
------------

[](#requirements)

- PHP 7.4+
- WordPress 5.0+

License
-------

[](#license)

GPL-2.0-or-later

Support
-------

[](#support)

- [Documentation](https:github.com/arraypress/wp-math-utils)
- [Issue Tracker](https:github.com/arraypress/wp-math-utils/issues)

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance44

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity13

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/cd6eb8aff0903d87eb674d1ba3c5f3653899c0d7661504eb0deb7798ed86b643?d=identicon)[arraypress](/maintainers/arraypress)

---

Top Contributors

[![arraypress](https://avatars.githubusercontent.com/u/22668877?v=4)](https://github.com/arraypress "arraypress (6 commits)")

### Embed Badge

![Health badge](/badges/arraypress-wp-math-utils/health.svg)

```
[![Health](https://phpackages.com/badges/arraypress-wp-math-utils/health.svg)](https://phpackages.com/packages/arraypress-wp-math-utils)
```

###  Alternatives

[ergebnis/phpstan-rules

Provides rules for phpstan/phpstan.

4428.9M187](/packages/ergebnis-phpstan-rules)[symfony/ldap

Provides a LDAP client for PHP on top of PHP's ldap extension

1407.5M46](/packages/symfony-ldap)[sylius/registry

Services registry.

6211.0M40](/packages/sylius-registry)[typo3/cms-scheduler

TYPO3 CMS Scheduler - Schedule tasks to run once or periodically at a specific time.

168.8M157](/packages/typo3-cms-scheduler)[laramall/laravel-system-info

查看服务器所有信息 system info

311.0k1](/packages/laramall-laravel-system-info)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
