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

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

lamansky/fraction
=================

A class that represents a fraction. Supports conversion to/from floats, mathematical operations, negative fractions, and Unicode stringification.

1.0.0(7y ago)661.9k↓84.7%MITPHPPHP &gt;=7.1.0CI failing

Since Sep 5Pushed 5y ago1 watchersCompare

[ Source](https://github.com/lamansky/fraction)[ Packagist](https://packagist.org/packages/lamansky/fraction)[ RSS](/packages/lamansky-fraction/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Fraction
========

[](#fraction)

A PHP class that represents a fraction. Converts to/from floats (0.25 ↔ ¼), simplifies fractions (⁴⁄₈ → ½), handles mathematical operations (½ + ⅓), supports negative fractions (−⅘), and does Unicode string output. Requires PHP 7.1 or above.

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

[](#installation)

With [Composer](http://getcomposer.org) installed on your computer and initialized for your project, run this command in your project’s root directory:

```
composer require lamansky/fraction
```

Requires PHP 7.1 or above.

API
---

[](#api)

The library consists of a single class: `Lamansky\Fraction\Fraction`.

### Constructor Parameters

[](#constructor-parameters)

1. `$a` (int or float): The numerator of the fraction (the number on top).
2. `$b` (int or float): The denominator of the fraction (the number on bottom).
3. Optional: `$negative` (bool or int): If set to `true` or `-1` (or any negative number), the fraction will be negative. If set to `false` or `1` (or any positive number), the fraction will be positive. If omitted, the fraction will be negative only if `$a` or `$b` is negative (but not both). If provided, the value of `$negative` will override whatever sign values `$a` or `$b` may have.

### Static Method: `fromFloat () : Fraction`

[](#static-method-fromfloat---fraction)

Accepts one parameter (a `float` number) and returns a `Fraction`. The `Fraction` will have the same sign value (positive/negative) as the float.

### `isNegative () : bool`

[](#isnegative---bool)

No parameters. Returns `true` if the fraction is negative; otherwise `false`.

### `getSignMultiplier () : int`

[](#getsignmultiplier---int)

No parameters. Returns `-1` if the fraction is negative, or `1` if it is positive.

### `getNumerator () : int`

[](#getnumerator---int)

No parameters. Returns the numerator of the fraction (the number on top).

### `getMixedInteger () : int`

[](#getmixedinteger---int)

No parameters. Returns the integer component of a mixed fraction. A mixed fraction is one which is simplified to use a whole number (e.g. ⁵⁄₄ → 1¼). Example:

```
$f = new Fraction(7, 2);
echo $f->toString(); // 3 1/2
echo $f->getMixedInteger(); // 3
echo $f->getMixedNumerator(); // 1
echo $f->getDenominator(); // 2
```

If the fraction is not mixed (i.e. if the numerator is smaller than the denominator), this function will return `0`.

### `getMixedNumerator () : int`

[](#getmixednumerator---int)

No parameters. Returns the numerator of a mixed fraction. A mixed fraction is one which is simplified to use a whole number (e.g. ⁵⁄₄ → 1¼). Example:

```
$f = new Fraction(5, 4);
echo $f->getNumerator(); // 5
echo $f->getMixedNumerator(); // 1
```

If the fraction is not mixed (i.e. if the numerator is smaller than the denominator), this function will return the normal numerator.

### `getDenominator () : int`

[](#getdenominator---int)

No parameters. Returns the denominator of the fraction (the number on bottom).

### `getParts () : array`

[](#getparts---array)

No parameters. Returns an array with two elements: the numerator and the denominator.

### `getMixedParts () : array`

[](#getmixedparts---array)

No parameters. Returns an array with three elements: the mixed-fraction integer, the mixed-fraction numerator, and the denominator. For example: for the fraction 2¼, it would return `[2, 1, 4]`.

### `toString () : string`

[](#tostring---string)

No parameters. Returns an ASCII string representation of the fraction.

```
$f = new Fraction(-5, 4);
echo $f->toString(); // '-1 1/4'
```

### `toUnicodeString () : string`

[](#tounicodestring---string)

No parameters. Returns a Unicode string representation of the fraction.

```
$f = new Fraction(-5, 4);
echo $f->toUnicodeString(); // '−1¼'
```

### `toFloat () : float`

[](#tofloat---float)

No parameters. Divides the numerator by the denominator and returns a floating-point number.

```
$f = new Fraction(-5, 4);
echo $f->toFloat(); // -1.25
```

### `clone () : Fraction`

[](#clone---fraction)

No parameters. Returns a `Fraction` with the same numerator, denominator, and positive/negative sign.

### `absolute () : Fraction`

[](#absolute---fraction)

No parameters. Clones the `Fraction`, but makes it positive if it’s negative.

### `add (Fraction $other) : Fraction`

[](#add-fraction-other--fraction)

Returns a `Fraction` that is the sum of the current fraction and `$other`.

Note that if `$other` is a negative fraction, this will end up being subtraction (just like in math).

### `subtract (Fraction $other) : Fraction`

[](#subtract-fraction-other--fraction)

Subtracts `$other` from the current fraction and returns the result.

### `multiply (Fraction $other) : Fraction`

[](#multiply-fraction-other--fraction)

Multiplies the current fraction by `$other` and returns the result.

### `divide (Fraction $other) : Fraction`

[](#divide-fraction-other--fraction)

Divides the current fraction by `$other` and returns the result.

Unit Tests
----------

[](#unit-tests)

To run the development test suite, execute this command:

```
./vendor/phpunit/phpunit/phpunit tests
```

###  Health Score

33

—

LowBetter than 73% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

2834d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3c526e960850903d0e2530bbfaac68c57d3d4cfb9df0cbed04435e74ac98a895?d=identicon)[lamansky](/maintainers/lamansky)

---

Top Contributors

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

---

Tags

mathnumbersfloatsfractions

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k532.9M373](/packages/brick-math)[markrogoyski/math-php

Math Library for PHP. Features descriptive statistics and regressions; Continuous and discrete probability distributions; Linear algebra with matrices and vectors, Numerical analysis; special mathematical functions; Algebra

2.4k7.4M47](/packages/markrogoyski-math-php)[kwn/number-to-words

Multi language standalone PHP number to words converter. Fully tested, open for extensions and new languages.

4275.3M23](/packages/kwn-number-to-words)[phpseclib/bcmath_compat

PHP 5.x-8.x polyfill for bcmath extension

16721.2M25](/packages/phpseclib-bcmath-compat)[drupol/phpermutations

Generators and iterators, permutations and combinations.

83713.5k5](/packages/drupol-phpermutations)[rubix/tensor

A library and extension that provides objects for scientific computing in PHP.

2801.5M5](/packages/rubix-tensor)

PHPackages © 2026

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