views:

118

answers:

5

In our Learning Management System someone in their infinite wisdom decided to keep non-standardized grades. As a result we have a table similar to this:

Assignment 1 - 100 Assignment 2 - 80 Assignment 3 - 10/20 Assignment 4 - 68 Assignment 5 - 8/10

As you can see we have a mixture of percentages and fractions. What i'd like to do is check if the grade is a fraction i.e. 10/20 and if so convert it out to a percentage. Are there any built in php functions for either action? I was thinking of doing a strpos('/'/, $grade); to check if it was a fraction but is there a cleaner way? Additionally to break up the fraction and convert it to a decimal my initial thought was to explode the fraction grade on a / and do (array[1] * 100) / array[2].

Is there any better solution than the one i am thinking?

A: 

Oh, thats a nice one, you can do it "easy" via regex:

$lines = array(
    "10/20",
    "20/10",
    "90",
);

foreach($lines as $line){
    if(preg_match('#([\d]+)/([\d]+)#',$line,$matches)){
        print (($matches[1]*100)/$matches[2])."% ";
    }else{
        print $line."% ";
    }

}

returns 50% 200% 90%

Hannes
Easy and Regex can never come in the same sentence, even if it's quoted. :)
medopal
it depends, there are regex's (like this one) a Human being can read and still comprehend, and then there are some .... well, others :D
Hannes
@Hannes, []'s are not needed, just `(\d+)`
stereofrog
@stereofrog ... yeah i know its a (bad) habbit
Hannes
+1  A: 

Array index starts at zero, not one.

$array = explode('/', $str, 2);
if (count($array) === 2) {
    $grade = sprintf('%.2f', 100 * $array[0] / $array[1]);
} else {
    $grade = $str;
}
Alex Howansky
+1  A: 
if(is_nan($grade)) {
     if(strpos('/',$grade) !== false) {
          $numbers = explode($grade,'/');
          $percent = (((int)$numbers[0] / (int)$numbers[1])*100).'%';
     } else {
          echo "Not a valid grade!";
     }
} else { 
     $percent = $grade.'%';
}

i believe that should work

dont have to deal with pesky regex either

Ascherer
So far this one looks to be the most ideal. Hence voted up. I'll vote the othe correct ones up to but this looks like the best one to accept. Anyone have comments to the contrary? Please feel free to call me out if you think you see a better answer here. Alex Howansky's also looks solid but this one checks first if its a fraction.
CogitoErgoSum
technically, it checks to see if its not a number, not a fraction. So if one of the grades is A and not 100 or 100/100, it'll break, which could be an advantage to a different method, although some logic could be used after the is_nan to check to make sure there is actually a / in it to make it more robust.
Ascherer
added the logic above
Ascherer
@CogitoErgoSum: don't say "i vote up the correct ones". Say "i vote up what i like the most". All answers so far are correct.
stereofrog
+1  A: 

A quick function which you can just pass the values to:

function normal($number)
{
    $parts = explode("/", $number);
    return count($parts) > 1 ? ($parts[0] * 100) / $parts[1] : $parts[0];
}
evolve
A: 

I'd do it like such:

public function convertFractions($givenValue){
        if(strpos($givenValue, "/") !== false){
            $strings = explode("/", $givenValue);
            return 100 * ($strings[0] / $strings[1]);
        } else {
            return $givenValue;
        }
    }

My one caveat would be: I'm not sure if the backslash requires escaping, as I've done here, as I didn't have time to test completely. If not, remove the backslash, and you should get the required values from the function every time.

EricBoersma
@EricBoersma Why a \? Fractions utilize the '/'. I had seen this approach before and its largely what i wrote out step by step.
CogitoErgoSum
Because I wasn't thinking correctly. I'll edit it to use the correct value now.
EricBoersma
@CogitoErgoSum If that's the solution that you chose for your implementation, it's often a good idea to either answer your own question, or to mark this as the correct one, for future people who come along.
EricBoersma