Make Calculation : <?php echo ''.$Total.'' ?> - php

How would l take the following and make a calculation that would be :
TOTAL * 2 / 100 and SHOW in decimal place (£) pounds.
So example 1000 (TOTAL) would be £20.00

£<?php echo number_format( ($total * 2) / 100, 2); ?>

number_format — Format a number with grouped thousands
http://php.net/manual/ru/function.number-format.php

Related

PHP - Add percentage to bitcoin values

I have a simple code in php and would like to add 3% on bitcoin value:
</PHP
$price = "0.00001000"
$price_format = str_replace(".", "", $price);
echo ($price_format + ($price_format / 100 * 3)); // 1030
?>
Return of my code:
1030
I need the return to be:
0.0001030
Does anyone know how I can perform this calculation by following the number of houses of the price?
You can use number_format when you multiply on the 3 percent to keep decimal places intact. Multiply price by 1.03 (3%) and specify 8 decimal places.
<?php
$price = 0.00001000;
$new_price = number_format($price * 1.03, 8);
echo $new_price;
Here's a link to number_format for more info:
https://www.php.net/manual/en/function.number-format.php

How to add between two numbers +1 ticket

i have database list for users and user's coupons and i want to add +1 ticket per coupon if bigger than two between numbers.
For example:
100 between 200 = 1 ticket
200 between 300 = 2 ticket
300 between 400 = 3 ticket
.......
.......
1200 between 1300 = 12 ticket
i put photo for example:
My Code is :
$q=$db->query("SELECT DISTINCT client_id FROM kuponlar ORDER BY client_id LIMIT 20");
foreach($q as $cat){
echo '<li id="'.$cat['client_id'].'" class="files">';
echo 'User ID: '.$cat['client_id'].'';
echo '<ul class="sub-menu">';
$linkq=$db->query("SELECT * FROM kuponlar WHERE client_id='" . $cat['client_id'] . "'");
foreach($linkq as $link){
echo '<li>Coupon ID: '.$link['kuponid'].' - Coupon Price: '.$link['yatirimi'].' ₺ / Won Ticket: '.substr($link['yatirimi'], 0, 1).' </li>';
}
echo '</ul></li>';
}
As discussed in the comments;
So if the number is 12 000, you want 120 as a result? Basically divide by 100? – Qirel
Yes #Qirel this is perfect comment. i mean like this. (...) – Ismail Altunören
So put simply, you want to divide the number by 100. You must then floor it, to get a full integer and get rid of any decimal points.
floor($link['yatirimi']/100);
You would replace that with your substr(), making the full line
echo '<li>Coupon ID: '.$link['kuponid'].' - Coupon Price: '.$link['yatirimi'].' ₺ / Won Ticket: '.floor($link['yatirimi']/100).' </li>';
floor() docs
You can do this using floor():
http://php.net/manual/en/function.floor.php
floor — Round fractions down
So this should do the trick: floor($link['yatirimi'] / 100)
Use that in place of your substr.
you can user this: $wonTicketsCount = round(($link['yatirimi'] / 100 ) - 0.5); instead substr($link['yatirimi'], 0, 1).
Refering to your comments if you only use hundreds steps :
Modify this line :
substr($link['yatirimi'], 0, 1)
This line will always take the first number.
By : this one
This will take all numbers excepts the two lasts.
substr($link['yatirimi'],0,-2);
By keeping the substr, it will not work for number between 0-100,
It's better to use #Qirel's Anwser.

How to solve floating 0.01 value on percentage discount

I'm facing some troubles with .01 values at the end of calculation and i would like to ask if someone has passed trough this issue and can give me a hand to solve mine.
I have this situation:
$total = '319.00';
$discount = '99.00';
$percentage_discount = number_format((1+($discount/$total)) * 100 - 100, 2, '.', '');
echo $percentage_discount . " %<br>";
echo $discount . "<br>";
echo number_format($total * (1-($percentage_discount / 100)), 2, '.', ''); //echo total
Result:
31.03 %
99.00
220.01
The result i need is The right percentage to get the final total value with 220.00
I know that on Magento VAT calculation was a problem similar to this, the final decimals floating was an issue from the beginning and hard to solve, but maybe some experienced person has solves this.
Remove number_format() on $persentage_discount, i.e. leave it as is:
$percentage_discount = ((1 + ($discount / $total)) * 100 - 100);
Otherwhise you are making double round during calculations and final result is 220.0143 because of that. If you want to show $persentage_discount somewhere, use printf().
Update: just simplify the task and separate displays from real calculations:
<?php
$total = '319.00';
$discount = '99.00';
$percentage_discount = ($discount / $total) * 100;
$final = $total * (1-($percentage_discount / 100));
printf('%.2f<br>', $total);
printf('%.2f<br>', $discount);
printf('%.2f%%<br>', $percentage_discount);
printf('%.2f<br>', $final);
?>
output:
319.00
99.00
31.03%
220.00
intval() will do the trick. try it and let me know
echo $new=number_format($total * (1-($percentage_discount / 100)), 2, '.', '').""; //echo total
echo intval($new);

Add 20% tax to amount

I have a amount without 20% Tax in my country.. For example, 12,24 € it s without tax and 14,70 EUR it's with a TAX.
How can I add 20% to 12.24 EUR Amount in PHP?
My script is:
<? echo round($cena*(20/100)+$cena, 2);?>
But with this script is amount with TAX 14,40 EUR and in real it is 14,70 EUR.
Where is problem? And numbers with zero at start it don't calulcate.. It will show 0,- EUR.
Thanks.
Use this
$num = 12.24;
$percentage = 20;
$num += $num*($percentage/100);
$num = round($num, 1); // 4
$num = sprintf('%0.2f', $num);
echo $num;
As mentioned in comments, just multiply that number by 1.2. So:
<?php echo round($cena * 1.2, 2); ?>
if you use calculator to solve it, 14.70 is correct. to get 14.40
<?php echo round($cena * 0.18, 2); ?>
0.18 is 18%
But with this script is amount with TAX 14,40 EUR and in real it is 14,70 EUR. Where is problem?
The problem is with the format of your input, 12,24. It is treated as 12. This is probably because PHP expects the decimal point to be . and not ,.

How to calculate with php the total with a VAT percentage in PHP

Im trying to make a calculation with the following values:
Product cost (without VAT) = 12,40 ($product)
The VAT percentage = 21%, what I will store in the database as 0,21 ($vat_perc)
The VAT is 2,604 ($vat)
edit: The VAT is per product
When I try to get the total then I get 15,00 ($total)
What I did is the following:
$total = $product + $vat
This will echo 15.004
Then I use the number_format:
echo(number_format($total,2,',','.'));
This will print 15.00
But then I want to multiply the product with 2
So that will give the following calculation:
$total = $product * 2 + $vat
Then again I use the format:
echo(number_format($total,2,',','.'));
Then the total = 30,01
I tried serveral things like ROUND en INT, but with no succes.
What am I doing wrong in this? In know that the VAT is the evil one here but I have no idea how to fix this.
$tax = round( ($price / 100) * 3.8, 2);
tax is rounded price divided by the 100 to make a clear percentage. Multiplied by the wanted stack
then you do the addition to or from your price table.
Well good to have you on the phone - maybe we can solve this faster by phone. Thank god for phones!
Cheers mate!
Here are some examples of how the numbers are rounded with PHP functions.
$product = 12.40;
$vat = 2.644;
$total = ( $product + $vat ) * 2;
var_dump( $total ); // float(30.088)
var_dump( number_format($total,2,',','.') ); // string(5) "30,09", rounded
var_dump( round( $total, 2 ) ); // float(30.09), rounded
var_dump( sprintf('%0.2f', $total ) ); // string(5) "30.09", rounded
var_dump( floor( $total * 100 ) / 100 ); // float(30.08), not rounded
All three founctions ( number_format, round, sprintf ) will round the result, to avoid the rounding and discard the decimal part after two decimal points you can use the last example.
Your initial total is 15.004 so when you call number_format that gets rounded down to 15.00. Now when you multiply by 2 your total is 15.008 which number_format will round up to 15.01. The issue isn't with the addition it is with the multiplication by 2. number_format rounds to the nearest place which for your case would be 30.01.
If you want the number to be rounded down all the time use floor, like so:
$total = floor(($product * 200)) / 100 + $vat;
echo(number_format($total,2,',','.'));

Categories