David David - 1 year ago 58
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 Source
 $array['WinLoss'] = round(100 * ($array['GamesWon'] / ($array['TotalGamesLost'] + $array['GamesWon'])), 2);

would give you % of games won

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download