David David - 7 months ago 12
PHP Question

How to transform (KD) number into win percentage in PHP

I need to calculate a win loss ratio from my app in PHP (Laravel 5.2)

I have these two numbers:

$array['WinLoss'] = $array['GamesWon'] / $array['TotalGamesLost'];


Which gives me:

Win: 480

Loss: 400


I tried doing this in my view:

{{ round((float)$WinLoss * 100) . '%' }}


but it gives me 120%. Thats obviously not correct, it should be around 58%

How would I calculate to show around 58% not 120%?

Tom Tom
Answer
 $array['WinLoss'] = round(100 * ($array['GamesWon'] / ($array['TotalGamesLost'] + $array['GamesWon'])), 2);

would give you % of games won

Comments