Skip to content

Instantly share code, notes, and snippets.

@RundesBalli
Last active May 24, 2020 17:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RundesBalli/a987971322ce7122e223393901fd90ec to your computer and use it in GitHub Desktop.
Save RundesBalli/a987971322ce7122e223393901fd90ec to your computer and use it in GitHub Desktop.
Fractionizer

Fractionizer

Fractionizer is a simple number format function with quarter, half and three quarter fraction output.
See examples.md for examples.
By default the decimal and thousand separator are formatted in the european version: dot for thousands, comma as decimal point.

<?php
// https://gist.github.com/RundesBalli/a987971322ce7122e223393901fd90ec
/**
* Fractionizer
*
* A simple number format function with quarter, half and three quarter fraction output.
*
* @author RundesBalli <webspam@rundesballi.com>
* @copyright 2020 RundesBalli
* @version 1.0
* @license MIT-License
* @see https://www.php.net/manual/en/function.number-format.php
*
* @param mixed $number Integer or float number to be formatted. Strings will be formatted to float.
* @param int $decimals Decimal count, will be ignoref if $number is dividable by 0.25
* @param bool $decimalsIfInt Ignores $decimals if $number is an integer
* @param string $decPoint Character to separate decimal numbers from integers
* @param string $thousandSeparator Thousand separator
*
* @return string Formatted number
*/
function fractionizer($number, int $decimals = 0, bool $decimalsIfInt = FALSE, string $decPoint = ",", string $thousandSeparator = ".") {
/**
* Convert the input number to float.
*/
$number = floatval($number);
/**
* Check if the number has no decimals.
*/
if(fmod($number, 1) == 0) {
if($decimalsIfInt === FALSE) {
return number_format($number, 0, $decPoint, $thousandSeparator);
} else {
return number_format($number, $decimals, $decPoint, $thousandSeparator);
}
}
/**
* Check if the number is dividable by 0.25.
*/
if(fmod($number, 0.25) == 0) {
$mod = abs(fmod($number, 1));
if($mod == 0.25) {
$fraction = "¼"; // Vulgar Fraction One Quarter, U+00BC
} elseif($mod == 0.5) {
$fraction = "½"; // Vulgar Fraction One Half, U+00BD
} elseif($mod == 0.75) {
$fraction = "¾"; // Vulgar Fraction Three Quarters, U+00BE
}
/**
* It is unnecessary to check if $fmod == 0, because integers have been previously checked and returned.
*/
/**
* If the number is negative, it must be "rounded up".
*
* ceil(-2.75) == 2
* @see https://www.php.net/manual/en/function.ceil.php
*
* floor(2.75) == 2
* @see https://www.php.net/manual/en/function.floor.php
*/
if($number < 0) {
return (ceil($number) == 0 ? NULL : ceil($number)).$fraction;
}
return (floor($number) == 0 ? NULL : floor($number)).$fraction;
}
/**
* If the Number has no quarter, half or three quarter decimals, it will return without extra formatting.
*/
return number_format($number, $decimals, $decPoint, $thousandSeparator);
}
?>
fractionizer(2);                        // returns: 2
fractionizer(2, 2);                     // returns: 2
fractionizer(2, 2, TRUE);               // returns: 2,00
fractionizer(2, 2, TRUE, ".", ",");     // returns: 2.00
fractionizer(2.1);                      // returns: 2
fractionizer(2.1, 2);                   // returns: 2,10
fractionizer(2.1, 2, FALSE, ".", ",");  // returns: 2.10
fractionizer(2.25);                     // returns: 2¼
fractionizer(2.25, 2, TRUE);            // returns: 2¼
fractionizer(2.25, 2, TRUE, ".", ",");  // returns: 2¼
fractionizer(2.5);                      // returns: 2½
fractionizer(2.5, 2, TRUE);             // returns: 2½
fractionizer(2.5, 2, TRUE, ".", ",");   // returns: 2½
fractionizer(2.75);                     // returns: 2¾
fractionizer(2.75, 2, TRUE);            // returns: 2¾
fractionizer(2.75, 2, TRUE, ".", ",");  // returns: 2¾
fractionizer(-2);                       // returns: -2
fractionizer(-2, 2);                    // returns: -2
fractionizer(-2, 2, TRUE);              // returns: -2,00
fractionizer(-2, 2, TRUE, ".", ",");    // returns: -2.00
fractionizer(-2.1);                     // returns: -2
fractionizer(-2.1, 2);                  // returns: -2,10
fractionizer(-2.1, 2, FALSE, ".", ","); // returns: -2.10
fractionizer(-2.25);                    // returns: -2¼
fractionizer(-2.25, 2, TRUE);           // returns: -2¼
fractionizer(-2.25, 2, TRUE, ".", ","); // returns: -2¼
fractionizer(-2.5);                     // returns: -2½
fractionizer(-2.5, 2, TRUE);            // returns: -2½
fractionizer(-2.5, 2, TRUE, ".", ",");  // returns: -2½
fractionizer(-2.75);                    // returns: -2¾
fractionizer(-2.75, 2, TRUE);           // returns: -2¾
fractionizer(-2.75, 2, TRUE, ".", ","); // returns: -2¾
MIT License
Copyright (c) 2020 RundesBalli
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment