Conversion Between Fractions To Decimals And Decimals To Fractions In PHP

admin_img Posted By Bajarangi soft , Posted On 24-09-2020

Convert between Fractions to Decimals and Decimals to Fractions in PHP

Conversion_between_decimals_to_fractions_and_fractions_to_decimal

1.Converting  Fractions to Decimals.
2.Converting  Decimals to Fractions.


Let's learn

1.Converting  Fractions to Decimals.
Calculation for Converting Fraction to Decimal. 

multiple 2 with 4(denominator) and add with 3(numerator )

        2*4+3=11/4 =2.75


Below method implemented by above approach of  Converting Fraction to Decimal:

<?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;
}
?>


2.Converting  Decimals to Fractions.
Calculation for Converting Decimal to Fraction. 

1. Rewrite the decimal number number as a fraction (over 1)

    1.375 = 1.375/1
2. Multiply numerator and denominator  by 103 = 1000 to eliminate 3 decimal places.

    1.375 / 1×1000 / 1000=1375 /1000
3. Find the Greatest Common Factor (GCF) of 1375 and 1000 and reduce the fraction, dividing both numerator and denominator by GCF = 125

    1375÷125/1000÷125=11/8


Below method implemented by above approach of  Converting Decimal to Fraction:

<?php
function decimalToFraction($decimal)
{
    if ($decimal < 0 || !is_numeric($decimal)) {
        // Negative digits need to be passed in as positive numbers
        // and prefixed as negative once the response is imploded.
        return false;
    }
    if ($decimal == 0) {
        return [0, 0];
    }
    $tolerance = 1.e-4;
    $numerator = 1;
    $h2 = 0;
    $denominator = 0;
    $k2 = 1;
    $b = 1 / $decimal;
    do {
        $b = 1 / $b;
        $a = floor($b);// floor method removes numbers after the point
        $aux = $numerator;
        $numerator = $a * $numerator + $h2;
        $h2 = $aux;
        $aux = $denominator;
        $denominator = $a * $denominator + $k2;
        $k2 = $aux;
        $b = $b - $a;
    } while (abs($decimal - $numerator / $denominator) > $decimal * $tolerance);//this function returns absolute positive value of a number
    return " Fraction ".$numerator ."/".$denominator;
}
?>



Complete Code of Conversion between Fractions to Decimals and Decimals to Fractions  in php :

<?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)
{
    if ($decimal < 0 || !is_numeric($decimal)) {
        // Negative digits need to be passed in as positive numbers
        // and prefixed as negative once the response is imploded.
        return false;
    }
    if ($decimal == 0) {
        return [0, 0];
    }
    $tolerance = 1.e-4;
    $numerator = 1;
    $h2 = 0;
    $denominator = 0;
    $k2 = 1;
    $b = 1 / $decimal;
    do {
        $b = 1 / $b;
        $a = floor($b);// floor method removes numbers after the point
        $aux = $numerator;
        $numerator = $a * $numerator + $h2;
        $h2 = $aux;
        $aux = $denominator;
        $denominator = $a * $denominator + $k2;
        $k2 = $aux;
        $b = $b - $a;
    } while (abs($decimal - $numerator / $denominator) > $decimal * $tolerance);//this function returns absolute positive value of a number
    return $numerator . "/" . $denominator;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Conversion  between Fractions to Decimals and Decimals to Fractions</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container " style="margin-top: 70px">
    <div class="text-center">
        <h1>Conversion  between Fractions to Decimals and Decimals to Fractions</h1>
    </div>
    <br>
    <br>
    <h4>Converting Fractions to Decimals</h4>
    <div class="well">
        <?php echo fractionToDecimal('2 3/4');// 2.75?>
    </div>
    <h4>Converting  Decimals to Fractions</h4>
    <div class="well">
        <?php echo decimalToFraction(1.375);// 11/8 ?>
    </div>
    <br>
</div>
</body>
</html>


Related Post