I’m having an issue with the calculated output. I’m simply trying to divide numbers, but it’s not coming out correctly.
Let’s say $some_value = 1160.00, and $another_value = 1360.
<?php
$money_earned = $some_value;
$cost_proposed = $another_value;
$money_percentage = (($money_earned / $cost_proposed) * 100);
?>
The returned value is: 0.073529411764706
But the actual value should be: 85.29411764705882
Why is this giving me the incorrect output value?
EDIT: Here’s my true code:
$money_earned = calculate_cost($_GET['track']);
$cost_proposed = $res[0]['cost_proposed'];
$money_percentage = (($money_earned / $cost_proposed) * 100);
You have an error in your maths somewhere.
0.073529411764706 * 1360 = 100
Therefore what you actually have in code is:
Double check your code to see where you’re getting that equation from.
Edit: based on comments, you’re converting a string with a comma in it into an integer. This breaks at the comma, so you end with 1. Fits the maths perfectly.
Strip the commas and convert to integer to get it to work. Ideally, store all numbers as numbers, only add the commas when you display on screen.