Point Formula based on a dynamic amount - php

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);

Related

How to work out the percentage of how much someone has paid

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;

Working out the new price from a %

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!

Showing a finance payment estimaton

I am attempting to add a estimated payment on my website, getting stuck on the interest amount.
Here's what I'd like to do:
(These will vary based on the year of the car, here for demonstration purposes)
$asking = 16495;
$down = 2474;
$doc = 200;
$interest = .05;
$tax = .0635;
$term = 60;
Basically what I'm trying to do is:
$asking + $doc x $tax - $down x $interest / $term
In my finance calculator the payment comes out to $288.37.
On my site the payment is over $7000.
What am I not seeing?
$asking = '$'.number_format($row['asking']);
$down_amount = $row['asking'] * .15;
$rate = .05;
$term = 60;
$loan_amount = ($row['asking'] * 6.35 ) - $down_amount;
$payment_amount = $loan_amount / $term * $rate;
You need parentheses to indicate the order of operation.
I am not 100% sure this is the order you want, but it should be close.
(($asking + $doc) x $tax - $down) x $interest / $term
Remember the computer will do division and multiplication first, and then do addition and subtraction, unless you specify your own order with parentheses.
Also,
$loan_amount = ($row['asking'] * 6.35 ) - $down_amount;
Should probably be
($row['asking'] * 1.0635 )
if that is supposed to be 6.35%.
Four things:
Order of operations and parentheses -- using your formula without any parentheses, you won't get the right answer.
The annual interest rate needs to be divided by the number of payments per year.
You will want to figure out the size of payments to amortize the loan to zero after 60 payments. Auto loans usually use actual/365 accrual.
VERY IMPORTANTLY,
Due to the Truth-in-Lending Act and Regulation Z, you need to be careful to have an adequate disclaimer.

understanding phpexcel RATE function

The Formula call for variables. I've look all over and can't find what these variables are and what values they're supposed to represent.
RATE($nper, $pmt, $pv, $fv = 0.0, $type = 0, $guess = 0.1)
Is anyone familiar with this function? Do you know what values the variables are meant to represent?
From the Excel help file:
RATE(nper,pmt,pv,fv,type,guess)
For a complete description of the arguments nper, pmt, pv, fv, and
type, see PV.
Nper - is the total number of payment periods in an annuity.
Pmt - is the payment made each period and cannot change over the life of the annuity. Typically, pmt includes principal and interest
but no other fees or taxes. If pmt is omitted, you must include the fv
argument.
Pv - is the present value — the total amount that a series of future payments is worth now.
Fv - is the future value, or a cash balance you want to attain after the last payment is made. If fv is omitted, it is assumed to be 0 (the
future value of a loan, for example, is 0).
Type - is the number 0 or 1 and indicates when payments are due.
Set type equal to the following if payments are due:
0 or omitted - At the end of the period
1 - At the beginning of the period
Guess - is your guess for what the rate will be.
If you omit guess, it is assumed to be 10 percent.
If RATE does not converge, try different values for guess. RATE usually converges if guess is between 0 and 1.
Remark
Make sure that you are consistent about the units you use for
specifying guess and nper. If you make monthly payments on a four-year
loan at 12 percent annual interest, use 12%/12 for guess and 4*12 for
nper. If you make annual payments on the same loan, use 12% for guess
and 4 for nper
I dont know if you need to implement this function, but in any case, I looked at how this algorithm was built and even though I was not able to access the excel source code (or the google worksheet) I found that this is not a simple calculation. About this math, more can be read here:
https://brownmath.com/bsci/loan.htm#Eq8
The function, in PHP, may be something like this:
function rate($nprest, $vlrparc, $vp, $guess = 0.25) {
$maxit = 100;
$precision = 14;
$guess = round($guess,$precision);
for ($i=0 ; $i<$maxit ; $i++) {
$divdnd = $vlrparc - ( $vlrparc * (pow(1 + $guess , -$nprest)) ) - ($vp * $guess);
$divisor = $nprest * $vlrparc * pow(1 + $guess , (-$nprest - 1)) - $vp;
$newguess = $guess - ( $divdnd / $divisor );
$newguess = round($newguess, $precision);
if ($newguess == $guess) {
return $newguess;
} else {
$guess = $newguess;
}
}
return null;
}

Calculate amount of $ before fees - PHP

I'm trying to make a small calculator, as a personal project. I have a little example here, what I want:
The user(me) get paid 10$ from a customer. What I do, is first to subtract the percentage which is 2.75% so after the subtraction I will have 9.725$ back. Then I take 1.5$ in fee. So I will be down on 8.225$ in the end.
That one works as it should.
What I then want to calculate is.
If I want 10$ AFTER fees are paid, (the 2.75% and 1.5$), how much should the customer then pay? and how could I do the calculation, so I could use any amount of moneys I want to receive.
Hope any can solve this 'easy' math problem.
A little bonus I would like to do also, is, if I edit the percentage to 3% etc, it should work too. (If impossible, then never mind).
I'm feeling really lost here at the moment!
Here are what I currently do, but it just adds 2.75% and the 1.5$ to the amount I specified.
function retPercent($amount, $percent)
{
return $amount + ($amount / 100 * $percent);
}
The problem here, is that I still don't know how to calculate the percentage I actually should use (based on the 2.75%) that I get from the substraction.
Thank you a lot guys and girls!
$result = ($value + 1.5) / 0.9725;
Or more generally:
function myFunc($value, $percentage, $fee) {
return ($value + $fee) / ((100-$percentage) / 100);
}
Other way round:
function myFuncTwo($result, $percentage, $fee) {
return (((100-$percentage) / 100) * $result) - $fee;
}
Example Use:
echo myFunc('8.225', '2.75', '1.5'); //10
echo myFuncTwo('10', '2.75', '1.5'); //8.225

Categories