I am trying to multiply numbers by 2 and these numbers are either whole numbers or fractions. I am pulling in these numbers dynamically through ACF fields on WordPress as text field. But some of these dynamic numbers are not fractions and just whole numbers such as 3 or 1. What I am trying to do is double the number so 3/4 would be 1 1/2. But when I multiply the fraction such as 3/4, it turns into 1.5. I used fubars code to get these numbers to stay as fractions but then I run into the issue of proper fractions showing up as decimals.
When you click 6 in the serving size, it should double the recipe as fractions.
Here is a code snippet from my template taking care of these numbers (I have javascript just hiding/showing the number on click depending on the serving size):
<?php
$number = get_sub_field('number'); // some numbers are 1/2 and 3/4 and some are 1 or 3
$double = 2;
$result = $number * $double;
?>
<?php echo $result; ?>
Would it be best for these to have a value as decimals but somehow use jQuery/javascript to convert it to fractions for front-end only?
Thoughts? I would really appreciate it!
Let me know.
Thanks!
EDIT
For those working with ACF fields via WordPress, heres a simple solution for this. The $number field is a number ACF field, so working with decimals to begin with.
<!-- Create the function -->
function convert($number) {
<!-- pull in the number field and multiply it by 2 (since we are
doubling -->
$number = $number * 2;
<!-- Create and Array of all the possibilities of fractions (this
instance is cooking measurements -->
$fractions = array(
0625 => '1/16',
1875 => '3/16',
3125 => '5/16',
4375 => '7/16',
5625 => '9/16',
6575 => '11/16',
8125 => '13/16',
875 => '14/16',
9375 => '15/16',
125 => '1/8',
33 => '1/3',
25 => '1/4',
66 => '2/3',
5 => '1/2',
375 => '3/8',
625 => '5/8',
75 => '3/4'
);
<!-- explode the part after the decimal point and convert it to the
fraction above -->
$parts = explode('.', $number);
if(isset($parts[1])) {
echo $parts[0].' '.$fractions[$parts[1]];
} else {
echo $number;
}
}
Then just add convert() around the $number value that is being pulled onto the WP template so convert($number).
When your number field contains a fractional value, PHP will actually consider it to be a string
, not a number
, and it'll therefore need to be converted to a number
before you can use it to perform arithmetic.
Since you've updated the requirements of your question, I've modified my answer to include two functions. I've written one function to convert from a fractional to decimal value, and another to convert from a decimal to a fractional value.
You can see a working example of this here: https://3v4l.org/ft0ZT
<?php
function fractionToDecimal($fraction)
{
// Split fraction into whole number and fraction components
preg_match('/^(?P<whole>\d+)?\s?((?P<numerator>\d+)\/(?P<denominator>\d+))?$/', $fraction, $components);
// Extract whole number, numerator, and denominator components
$whole = $components['whole'] ?: 0;
$numerator = $components['numerator'] ?: 0;
$denominator = $components['denominator'] ?: 0;
// Create decimal value
$decimal = $whole;
$numerator && $denominator && $decimal += ($numerator/$denominator);
return $decimal;
}
function decimalToFraction($decimal)
{
// Determine decimal precision and extrapolate multiplier required to convert to integer
$precision = strpos(strrev($decimal), '.') ?: 0;
$multiplier = pow(10, $precision);
// Calculate initial numerator and denominator
$numerator = $decimal * $multiplier;
$denominator = 1 * $multiplier;
// Extract whole number from numerator
$whole = floor($numerator / $denominator);
$numerator = $numerator % $denominator;
// Find greatest common divisor between numerator and denominator and reduce accordingly
$factor = gmp_intval(gmp_gcd($numerator, $denominator));
$numerator /= $factor;
$denominator /= $factor;
// Create fraction value
$fraction = [];
$whole && $fraction[] = $whole;
$numerator && $fraction[] = "{$numerator}/{$denominator}";
return implode(' ', $fraction);
}
// Examples
var_dump(fractionToDecimal('1/25')); // 0.04
var_dump(fractionToDecimal('2 3/4')); // 2.75
var_dump(fractionToDecimal('6/4')); // 1.5
var_dump(decimalToFraction(1.375)); // 1 3/8
var_dump(decimalToFraction(3)); // 3
var_dump(decimalToFraction(2.875)); // 2 7/8