I have tried to do this myself, it seems fairly simple i must be over complicating it, i have the following code:
// get the % we make
$row = DB::getInstance()->selectValues('SELECT * FROM `profit`');
$per = $row['profit_percentage'];
$pro = $per * 100 - ((str_replace("$", " ", trim($offer->children('campinfo', true)->amount) / 100)));
The value of $per = 60 this value $offer->children('campinfo', true)->amount can contain any value like: 0.55 or 25.34 what i'm trying to do is deduct 60 (the percentage) off the value of $offer->children('campinfo', true)->amount
I'm going round in circles
any help would be appreciated.
This boils down to simple math:
Giving a 60% discount is the same as only charging 40%.
So all you have to do is calculate $price * 0.4.
Pro tipp: Don't store your percentage as {0..100}, but as {0..1}. That way you can rewrite your code as (1 - $per) * $price!
Related
I have been trying to google this for hours but am finding it hard to find the maths to achieve this.
If the total payment is 200 GBP and the user has paid 100 GBP - he has paid 50% of the total.
I am trying to translate this to an algorithm but cannot get the expected result.
I have tried:
($total / $paid) * 100;
(($total + $paid) / 100) * 2;
($total - $paid) / 100;
But neither of this is giving me 50% when I use 200 as the total payment and 100 as the amount paid. Any help would be appreciated. I'm not very good at maths.
Simply do:
$percent = 100 * $paid/$total;
To get the percent value up to two decimal places only, you can use the Round() function:
$percent = ROUND(100 * $paid/$total, 2);
It is very simple just do this $percentPaid = $paid/$total * 100;
When I need to add a percentage to a price in order to increase it by a certain percentage I have this formula $price * (1 + $percent / 100) which is somewhat limiting.
For example if I wanted to increase the price by 200% or 300% I'd need to create a complex code to detect if its a 3 decimal number. And then extract the first number and replace the 1 like this (2 + $percent / 100).
Is there a more elegant way to increase a price by a certain percentage?
This is my way to do:
$price = $price + ($price * $percent / 100)
Works simple, calculate te percent of the price ($price * $percent / 100) and then sums that to the initial price.
This should work on every case
$price = $price + ($percentage / 100 ) * $price
But your formula is working with 200 or 300 without problem
How can I write this equation.
Pretty much here is what I have
(float) $total = (float) $product_price * (float) $percentage;
return (float) $total;
Although now I need to add a country percentage. Therefore if the country percentage is 10% I need to add 10% onto the current total and return it.
I use the variable $country_percentage
If I understand correctly, you want to add the percentage (tax?) to the total. If the $country_percentage is the integer '10', that would equate to '0.10'
($total * (1 + ($country_percentage / 100))
Which would be ($total * 1.10)
You use math, my friend. If your percentage is a string, remove the % symbol with str_replace() and then parse that value using intval($int) or floatval($float). That'll give you a number to work with, 10 for example.
Then you divide your country percentage by 100
10/100 = 0.1
Finally, you multiply your product price by the above value +1.
So:
//if $percentage is a string with '%' in it
$percentage = (floatval( str_replace("%", $percentage) );
$percentageNum = ($percentage/100) + 1;
$total = $product_price * $percentageNum;
i have a database with the current tax value (it is given in percentages to add like 41, 51 etc lets say it is 41 $tax = 41) The tax comes from the database the price is a post variable.
What i want to do is calcutate the new price when the tax is added.
Currently i have.
$price = 120;
$tax = 41;
$total = (($price *100 + $price * $percentage)/100);
Is there any way to make it easyer? like:
$total = $price * 1.$tax
i know the last one wont work but its just what i had in mind.
What is the best solution?
You can calculate the percentage when querying database or go for
$total = $price * (1 + $tax/100);
I'm trying to create a dynamic point system based off of how much money is available in a bank account. For instance, if someone has 20 points and we have 100 dollars in the bank account, I want the formula to return something along the lines of $1.
I don't care too much about the ratio at this point - more about the formula to get there.
So far, I've come up with a handful of different formulas,
$val = (1 / $this->bank) * $amt; //goes the wrong way
$val = ($amt / $this->bank) * 5000; //isn't a good ratio
Assume val is the amount you can get with all the points, bank is how much is in the bank, and $amt is the amount of points the user has.
A gentle nudge in the right direction would be helpful!
Thanks!
$bank * ($points / 2000)
This satisfies your test case, giving 1 for $bank = 100 and $points = 20.
Saying $percentage is the percentage you want between your points and the bank account amount to have 1$ reward, you will have:
$percentage = 20;
$val = $amt * $this->bank / ($percentage * 100);