PHPackages                             saki/fraction - 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. saki/fraction

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

saki/fraction
=============

3.0.0(1y ago)102MITPHPPHP &gt;=8.4

Since Dec 4Pushed 1y ago1 watchersCompare

[ Source](https://github.com/SakiTakamachi/Fraction-for-PHP)[ Packagist](https://packagist.org/packages/saki/fraction)[ RSS](/packages/saki-fraction/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (6)Used By (0)

Fraction
========

[](#fraction)

[![Latest Stable Version](https://camo.githubusercontent.com/57b93275257c699e2a75dcba6a99195372dd9ee7127e4cc6b55e4bba9ea45e54/68747470733a2f2f706f7365722e707567782e6f72672f73616b692f6672616374696f6e2f76)](https://packagist.org/packages/saki/fraction)[![Push](https://github.com/SakiTakamachi/Fraction-for-PHP/actions/workflows/push.yml/badge.svg)](https://github.com/SakiTakamachi/Fraction-for-PHP/actions/workflows/push.yml)[![codecov](https://camo.githubusercontent.com/c6f65c36eead8eae00a77302c70b116475545bbd96b1570b4f597f9393e5228c/68747470733a2f2f636f6465636f762e696f2f67682f53616b6954616b616d616368692f4672616374696f6e2d666f722d5048502f67726170682f62616467652e7376673f746f6b656e3d344d494d324c52505244)](https://codecov.io/gh/SakiTakamachi/Fraction-for-PHP)[![License](https://camo.githubusercontent.com/c12a76a10f1a276fe9cf18b1b6416e63be880e0b679b11718aa23f26e210bed2/68747470733a2f2f706f7365722e707567782e6f72672f73616b692f6672616374696f6e2f6c6963656e7365)](https://packagist.org/packages/saki/fraction)

Install
-------

[](#install)

For PHP 8.4:

```
composer require saki/fraction: "3.*"

```

For PHP 8.3 and 8.2:

```
composer require saki/fraction: "2.*"

```

This library requires the BCMath extension.

Description
-----------

[](#description)

This is a PHP library that handles fractions using `BcMath\Number`. The class is `final` and `readonly`, so an immutable object.

### Create a object

[](#create-a-object)

The constructor is:

```
// __construct(Number|int|string $numerator, Number|int|string $denominator = 1)

use Saki\Fraction;

$fraction = new Fraction(1, 2); // 1/2
$fraction = new Fraction('0.5', -1); // -1/2
$fraction = new Fraction('-3'); // -3/1
$fraction = new Fraction('-2', '-4'); // 1/2

```

You can also create objects from strings representing fractions like `"1/2"`.

```
$fraction = Saki\Fraction::createFromFractionString('1/2');

```

### Methods

[](#methods)

The available methods are:

- `add(Fraction|Number|int|string $num): Fraction`
- `sub(Fraction|Number|int|string $num): Fraction`
- `mul(Fraction|Number|int|string $num): Fraction`
- `div(Fraction|Number|int|string $num): Fraction`
- `mod(Fraction|Number|int|string $num): Fraction`
- `divmod(Fraction|Number|int|string $num): Fraction[]`
- `pow(Number|int|string $num): Fraction`
- `compare(Fraction|Number|int|string $num): int`
- `toNumber(): Number`

Additionally, this class is `Stringable` and supports serialization.

### Convert to `BcMath\Number`

[](#convert-to-bcmathnumber)

When converting a `Fraction` to `BcMath\Number`, for example, a value like `"1/10000000000"` was too small and could end up as `"0"`. Therefore, in this case, the numerator is `1` digit and the denominator is `11` digits, so the division is executed with `BcMath\Number::scale` extended by the `10` digits of the difference. Division of `BcMath\Number` automatically extends `BcMath\Number::scale` by up to another `10` digits from this state, so you can obtain sufficient precision.

e.g.

```
// Example of indivisible division

use Saki\Fraction;

$fraction = new Fraction(1, 30000);
var_dump($fraction->toNumber());

```

output:

```
object(BcMath\Number)#2 (2) {
  ["value"]=>
  string(16) "0.00003333333333"
  ["scale"]=>
  int(14)
}

```

### Cast to `string`

[](#cast-to-string)

When cast to `string`, it becomes a `string` representing a fraction, such as `"-3/10"`.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 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.

###  Release Activity

Cadence

Every ~1 days

Total

5

Last Release

526d ago

Major Versions

1.0.0 → 2.0.02024-12-09

2.0.x-dev → 3.0.02024-12-09

PHP version history (2 changes)1.0.0PHP &gt;=8.4

2.0.0PHP ^8.3|^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/53299f3c3e6b8654e99f64274bdaa41d8d54f063951d848ff4d0c9ecdac0a7eb?d=identicon)[SakiTakamachi](/maintainers/SakiTakamachi)

---

Top Contributors

[![SakiTakamachi](https://avatars.githubusercontent.com/u/34942839?v=4)](https://github.com/SakiTakamachi "SakiTakamachi (11 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/saki-fraction/health.svg)

```
[![Health](https://phpackages.com/badges/saki-fraction/health.svg)](https://phpackages.com/packages/saki-fraction)
```

PHPackages © 2026

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