{"id":22021544,"url":"https://github.com/lamansky/fraction","last_synced_at":"2025-05-07T06:41:18.320Z","repository":{"id":57010847,"uuid":"136667866","full_name":"lamansky/fraction","owner":"lamansky","description":"A PHP class that represents a fraction. Supports conversion to/from floats, mathematical operations, negative fractions, and Unicode stringification.","archived":false,"fork":false,"pushed_at":"2020-08-18T13:58:27.000Z","size":8,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-23T01:07:03.075Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lamansky.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-08T21:32:40.000Z","updated_at":"2022-08-19T01:37:58.000Z","dependencies_parsed_at":"2022-08-21T13:40:45.955Z","dependency_job_id":null,"html_url":"https://github.com/lamansky/fraction","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Ffraction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Ffraction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Ffraction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Ffraction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lamansky","download_url":"https://codeload.github.com/lamansky/fraction/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252830539,"owners_count":21810770,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-30T06:12:44.939Z","updated_at":"2025-05-07T06:41:18.302Z","avatar_url":"https://github.com/lamansky.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fraction\n\nA 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.\n\n## Installation\n\nWith [Composer](http://getcomposer.org) installed on your computer and initialized for your project, run this command in your project’s root directory:\n\n```bash\ncomposer require lamansky/fraction\n```\n\nRequires PHP 7.1 or above.\n\n## API\n\nThe library consists of a single class: `Lamansky\\Fraction\\Fraction`.\n\n### Constructor Parameters\n\n1. `$a` (int or float): The numerator of the fraction (the number on top).\n2. `$b` (int or float): The denominator of the fraction (the number on bottom).\n3. 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.\n\n### Static Method: `fromFloat () : Fraction`\n\nAccepts one parameter (a `float` number) and returns a `Fraction`. The `Fraction` will have the same sign value (positive/negative) as the float.\n\n### `isNegative () : bool`\n\nNo parameters. Returns `true` if the fraction is negative; otherwise `false`.\n\n### `getSignMultiplier () : int`\n\nNo parameters. Returns `-1` if the fraction is negative, or `1` if it is positive.\n\n### `getNumerator () : int`\n\nNo parameters. Returns the numerator of the fraction (the number on top).\n\n### `getMixedInteger () : int`\n\nNo 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:\n\n```php\n$f = new Fraction(7, 2);\necho $f-\u003etoString(); // 3 1/2\necho $f-\u003egetMixedInteger(); // 3\necho $f-\u003egetMixedNumerator(); // 1\necho $f-\u003egetDenominator(); // 2\n```\n\nIf the fraction is not mixed (i.e. if the numerator is smaller than the denominator), this function will return `0`.\n\n### `getMixedNumerator () : int`\n\nNo 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:\n\n```php\n$f = new Fraction(5, 4);\necho $f-\u003egetNumerator(); // 5\necho $f-\u003egetMixedNumerator(); // 1\n```\n\nIf the fraction is not mixed (i.e. if the numerator is smaller than the denominator), this function will return the normal numerator.\n\n### `getDenominator () : int`\n\nNo parameters. Returns the denominator of the fraction (the number on bottom).\n\n### `getParts () : array`\n\nNo parameters. Returns an array with two elements: the numerator and the denominator.\n\n### `getMixedParts () : array`\n\nNo 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]`.\n\n### `toString () : string`\n\nNo parameters. Returns an ASCII string representation of the fraction.\n\n```php\n$f = new Fraction(-5, 4);\necho $f-\u003etoString(); // '-1 1/4'\n```\n\n### `toUnicodeString () : string`\n\nNo parameters. Returns a Unicode string representation of the fraction.\n\n```php\n$f = new Fraction(-5, 4);\necho $f-\u003etoUnicodeString(); // '−1¼'\n```\n\n### `toFloat () : float`\n\nNo parameters. Divides the numerator by the denominator and returns a floating-point number.\n\n```php\n$f = new Fraction(-5, 4);\necho $f-\u003etoFloat(); // -1.25\n```\n\n### `clone () : Fraction`\n\nNo parameters. Returns a `Fraction` with the same numerator, denominator, and positive/negative sign.\n\n### `absolute () : Fraction`\n\nNo parameters. Clones the `Fraction`, but makes it positive if it’s negative.\n\n### `add (Fraction $other) : Fraction`\n\nReturns a `Fraction` that is the sum of the current fraction and `$other`.\n\nNote that if `$other` is a negative fraction, this will end up being subtraction (just like in math).\n\n### `subtract (Fraction $other) : Fraction`\n\nSubtracts `$other` from the current fraction and returns the result.\n\n### `multiply (Fraction $other) : Fraction`\n\nMultiplies the current fraction by `$other` and returns the result.\n\n### `divide (Fraction $other) : Fraction`\n\nDivides the current fraction by `$other` and returns the result.\n\n## Unit Tests\n\nTo run the development test suite, execute this command:\n\n```bash\n./vendor/phpunit/phpunit/phpunit tests\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2Ffraction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flamansky%2Ffraction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2Ffraction/lists"}