Rounding a price to closest multiple of 5 - php

I am working on the following code. Why am I not able to round the price to be always rounded up to nearest multiple of 5?
$fee = 364.99;
$discount = 25;
$price = round($fee - ($fee * ($discount / 100)))*5;
echo $price;
I am hoping to get 275.00 the output is 1370

You forget to divide by 5.
Correct calculation is
$price = round(($fee - ($fee * ($discount / 100))) / 5) * 5;
In general
$round = 5;
$calc = round($num / $round) * $round;

Related

calculate discount for total amount multiplied by 100

I'm having trouble calculating the discount of total order amount of products that is multiplied by 100.
In my database I'm saving the product price multiplied by 100 e.g.: $price = 33.5 * 100 I'm doing this to prevent wrong calculation of the decimal point .5 when retrieving the data and adding those to other products.
So I have this for now
$test_product_price = 10000 // actual price is 100 because it's save multiplied by 100
$quantity = 3;
$discount = 10.5%;
$order_amount = $test_product_price * 3 // will produce 30000 but actual price is 300 when divided by 100
Now what I want is deduct the $discount which is 10.5%
for actual amount this is working fine
$discounted_amount = 300 - (300 * (10.5 / 100)); // will produce 268.5
but how can I do that on my end that is my amounts are multiplied by 100
Possibly a bit long winded but accurate;
<?php
$amount = 100;
$number = 3;
$percent = 10.5;
$subtotal = $amount * $number;
$discount = ($percent / 100) * $subtotal;
$total = $subtotal - $discount;
$total = number_format((float)$total, 2, '.', '');
echo $total;
?>

Loop to Calculate Price

trying to create a loop to calculate a price but cant get my head around how to do it.
$sell = ($price*$vat);
$profit = ((($price*$vat)*0.04)+0.2);
function newSell($price1){
$price1 = $price1 + 0.10;
return $price;
}
do {
$price1 = newSell($price);
$profit = ((($price1*$vat)*0.04)+0.2);
} while ($profit < 0);
$price is the price of my item
$sell is my starting price.
$profit is the calcualtion to work out my profit.
What i want to do is look around and if my profit is less than 0 i want to add 10p (0.10) to my price and then recalculate my profit and evaluate it again. Want to keep going until my profit is above 1 at which point it stops and my new selling price has been set.
Cant for the life of me get my head around this!
Many thanks
First of all your newSell function should look like this:
function newSell($price) {
return $price + .1
}
It currently does nothing.
Second, are you sure this is the way you should be trying to solve this? It seems to me like you are just trying to solve for $profit >= 1. It's a simple equation that takes no loops whatsoever.
Apparently $profit = $price * $vat * .04 + .2, so you are really looking at 1 <= $price * $vat * .04 + .2 or 20 / $vat <= $price
So if $price >= 20 / $vat then $profit >= 1.
To do specifically what you asked:
do {
$profit = $price * $vat * .04 + .2;
$price = $price + .1;
} while ($profit < 1);

Calculate only value of vat

I trying to calculate but can't get this done, if someone can help
I have the total of the order and the vat at 23%
$vat = 23;
$total = 6.00;
For add the vat to total I will do
$total_with_vat = $total * $vat;
that will be 7.40
I now the total is 6.00, but how can I get the 1.40 of the vat
I need this to make some calculations. Looks simple but I'am not getting only to do it.
Thanks
Pretty simple
$vat = $total * $vat / 100.0;
// 1.38
If you'd like to guarantee 2 digits of precision, you can use round
$vat = round($vat, 2);
Make it into a reusable function
function calculateVat($amount, $vat=23) {
return round($amount * $vat / 100.0, 2);
}
$amount = 6.00;
$vat = calculateVat($amount);
// 1.38
If VAT is a different amount, you can specify it as a second argument
$amount = 6.00;
$vat = calculateVat($amount, 30);
// 1.8

Rounding a calculation up using php

How would I round this calculation up automatically??
$calc = (14.3/10)/2.33 Result = 6.137339055794
The value I would like to have is 6.13
Have searched the site but can't find any answers on this
Use PHP round().
$calc = round($calc, 2)
The result of (14.3/10)/2.33 is not 6.137339055794. It's 0.6137339055794.
I'm assuming: $calc = (14.3/10) / 2.33 * 10;.
The function round rounds half up, half down, half even or half odd numbers (floating point numbers), but the result of round($calc, 2, PHP_ROUND_HALF_UP) is not 6.13. It's 6.14.
Assuming you'd like to round down or truncate that number, a solution could be:
$truncated = (int)($calc * 100) / 100;
or
$precision = 2;
$tensPrecision = pow(10, $precision);
$truncated = (int)($calc * $tensPrecision) / $tensPrecision;
.
To round up, you could do:
$truncated = ceil($calc * 100) / 100;
or
$precision = 2;
$tensPrecision = pow(10, $precision);
$truncated = ceil($calc * $tensPrecision) / $tensPrecision;

Percentages in PHP

Not to sound stupid but I was wondering if there is a very simple way of taking a percentage from a number in PHP?
Say I have a number of 70.40 and I want to remove 15% of this.
So far I have something like:
$discount = 15;
$price = 70.40;
$newPrice = $price - (($discount / 100) * $price);
But this seems to be giving me all kinds of random numbers.
is there an easier way?
Try this:
function apply_discount($price, $discount) {
$after_discount = $price - (($price / 100) * $discount);
return $after_discount;
}
e.g.
echo apply_discount(200, 15); //170
$discount = 15;
$price = 70.40;
$newPrice = $price * (100 - $discount) / 100;
If your $discount would be a percentage (between 0 and 1) it would be:
$discount = 0.15;
$price = 70.40;
$newPrice = $price * (1 - $discount);
70.40 * 0.15 = 10.56
whereas 0.15 is 15%
I guess you could do something like this.
$discount = 0.15;
$price = 70.40;
$newPrice = $price - ($price * $discount);

Categories