Novice
Novice

Reputation: 13

getting round average in laravel way

I was trying to run average in my project but I was not able to make the round function work, every time I run the function I always end up with float value example: average is 86.5, I wanted it to round to nearest tens which is 87 or if lower it will turn 86.

Controller

$score->average =round( $row['result1'] + $row['result2']) /2;

Schema

 $table->integer('result1');
 $table->integer('result2');

Upvotes: 0

Views: 1319

Answers (3)

Sagar Gautam
Sagar Gautam

Reputation: 9389

You can get integer value like this:

$score->average = (int)round(($row['result1'] + $row['result2']) /2);

Since, round function returns floating point value like 3.0, 45.0 so to get integer you have to type case the value.

Upvotes: 1

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

You should try this:

$summation = $row['result1'] + $row['result2'];
$divideRslt = $summation / 2;

$score->average =round($divideRslt);

Upvotes: 0

Jovs
Jovs

Reputation: 892

Can you try this one

$score->average =round( ($row['result1'] + $row['result2']) /2) ;

using this will apply the PEMDAS rule.

Hope it helps

Upvotes: 0

Related Questions