0

Hi I have seen that stack overflow wrote the code for socialchef to covert their decimal input into fraction.

How can I write or add a code to input fraction and manta in as a fraction...
I really need assistance.

This is the code in the theme utils...

// code thanks to Bryan [https://stackoverflow.com/a/26684935][1]
public static function convert_decimal_to_fraction($decimal){

    $big_fraction = SocialChef_Theme_Utils::float_to_rat($decimal, 0.1);
    if ($big_fraction) {
        $num_array = explode('/', $big_fraction);
        if (count($num_array) > 1) {
            $numerator = $num_array[0];
            $denominator = $num_array[1];
            if ($denominator) {
                $whole_number = floor( $numerator / $denominator );
                $numerator = $numerator % $denominator;

                if($numerator == 0){
                    return $whole_number;
                }else if ($whole_number == 0){
                    return $numerator . '/' . $denominator;
                }else{
                    return $whole_number . ' ' . $numerator . '/' . $denominator;
                }
            }
        }
    }
    return 0;

(reviewer addition) : StackOverflow reference : https://stackoverflow.com/a/26684935

Community
  • 1
  • 1

1 Answers1

0

I stared at your question a long time...

What I this function does is:
- If its input is a fraction like 3/4, it will remain as 3/4.
- But if it's a fraction like 14/4, this function will modify it to be 3 2/4.

Now what you want is to maintain this big fraction instead of having it "reduced" this way.
If I'm correct on this, the answer is really simple.

$big_fraction = SocialChef_Theme_Utils::float_to_rat($decimal, 0.1);
if ($big_fraction) {

/*  // Just comment out this whole calculation block

    $num_array = explode('/', $big_fraction);
    if (count($num_array) > 1) {
        $numerator = $num_array[0];
        $denominator = $num_array[1];
        if ($denominator) {
            $whole_number = floor( $numerator / $denominator );
            $numerator = $numerator % $denominator;

            if($numerator == 0){
                return $whole_number;
            }else if ($whole_number == 0){
                return $numerator . '/' . $denominator;
            }else{
                return $whole_number . ' ' . $numerator . '/' . $denominator;
            }
        }
    }

*/  // End of commenting out.

return $big_fraction;   // Return the unmodified value.
}

So this simply disables this math helper from your Wordpress theme.

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64