Hi I need to remove 10% from a shopping carts subtotal
Original code:
<?php echo number_format($order->subtotal,2);?>&OID=<?php echo $order->trans_id;?>
I know it's not precise, but would something like this work?
<?php echo number_format($order->subtotal * 0.909090909,2);?>&OID=<?php echo $order->trans_id;?>
Thanks
Use sprintf()
$a = 2324.56*0.909090909 ;
echo sprintf('%0.2f',$a);
output // 2113.24
sprintf() will handle the floating point precession which is the best way to handle.
If needs to display the money format for specific locale it could be doing using money_format
$a = 2324.56*0.909090909 ;
$amount = sprintf('%0.2f',$a);
setlocale(LC_MONETARY, 'en_US');
echo money_format('%(#1n', $amount) . "\n";
output // $2,113.24
Here is an explanation on number_format() -ve value precession issue
http://www.howtoforge.com/php_number_format_and_a_problem_with_negative_values_rounded_to_zero
that probably would work, but why not just subtract the 10 percent? If that's the goal, why not just do it? keep in mind number_format rounds up, but I expect that's desired.
<?php echo number_format( ($order->subtotal - ($order->subtotal* .1) ) ,2);?>
This is the math that actually subtracts 10% why not use this instead of something that's close?
try this
$subtotal = $order->subtotal;
$cut_subtotal = $subtotal *(10/100);
$subtotal_new = $subtotal-$cut_subtotal;
Now use this in your code
<?php echo number_format($subtotal_new,2);?>
Related
I'm trying to format numbers with not decimal points or commas, I've tried number_format() and money_format() and can't seem to get the result I need.
number_format($item->amount,2)
Result: 14,995.00
money_format("%i", $item->amount)
Result: 14,995.00
I'm want to get the following numbers formatted correct.
14995 needs to be £149.95
6795 needs to be £67.95
What is the best way to get the result above?
Using brick/money (disclaimer: I'm the author):
use Brick\Money\Money;
// Instantiating from a decimal amount
$money = Money::of('67.95', 'GBP');
echo $money->formatTo('en_GB'); // £67.95
// Instantiating from a minor amount (cents)
$money = Money::ofMinor(6795, 'GBP');
echo $money->formatTo('en_GB'); // £67.95
setlocale(LC_MONETARY, 'en_GB.UTF-8'); // change if needed
echo money_format('%n', 6795 / 100);
returns
£67.95
for me, so that should work. As i mentioned in my comment, you probably can't get the code to both display 14995 as 1,499.50 (divide by 10) and 6795 as 67.95 (divide by 100), so I'd recommend refactoring the code accordingly.
I wanted to calculate the percentage using PHP. I tried the code given below but its gives me the return value in float. i don't know much in PHP so please fix this code.
current OUTPUT
66.666666666667%
Expected OUTPUT
66.66%
<?php
$up=4;
$down:2;
echo (($ups/($ups+$downs))*100).'%';
?>
Use number_format() to specify your decimals and separator.
<?php
$up=4;
$down:2;
$num = (($ups/($ups+$downs))*100).'%';
$formatted_num = number_format($num, 2, '.', '');
echo $formatted_num;
?>
You can do like this :
echo round(66.666666666667, 2); >> 66.66
I would like to show a number 1000000 in the format 10,00,000 in PHP.
What is the PHP function to do this conversion?
use number_format function
<?php
echo number_format(100000); // prints 1,000,000
?>
If you need to display a value formatted as Lakhs and Crores:
$amount = 1000000000;
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount;
should give 100,00,00,000 instead of 1,000,000,000
I'm new to PHP.
My code reads a price value from a Steam game's json data.
http://store.steampowered.com/api/appdetails/?appids=8870
Problem is that the value of the price node is not formatted with a comma separator for dollars and cents. My code works to piece together the dollars and cents but is it the right way to do it for this instance. Also if there is another easier method of doing my newbie code, feel free to show me where it can be improved. Thanks!
<?php
$appid = '8870';
$ht = 'http://store.steampowered.com/api/appdetails/?appids=' . $appid;
$fgc = file_get_contents($ht);
$jd = json_decode($fgc, true);
$gdata = $jd[$appid]['data'];
$gname = $gdata['name'];
$gprice = $gdata['price_overview']['final'];
$gdesc = $gdata['detailed_description'];
$gusd = substr($gprice, 0, -2);
$gcent = substr($gprice, 2);
echo $gname. '<br>';
echo 'Price: $' .$gusd. ',' .$gcent;
?>
If I may ask another question... can the price data aka $gprice be added to another price data that is fetched, to return a total.
I would essentially do what you are doing except its much simpler to just divide by 100:
Turn the price into a float:
$gprice = $gprice / 100;
and then use money_format
Ref: PHP Docs - money_format
You could also do this, but there isn't really a need.
$gprice = (int) $gdata['price_overview']['final'];
The conversion is not bad, but you could also use this:
$gusd = $gprice/100;
echo $gname. '<br>';
echo 'Price: $' .str_replace('.', ',', $gusd);
or use money_format instead of replace, but it's a little more complicated.
also, to add another you could do it with just the + or += operators like this:
$gprice+= $gdata['price_overview']['final'];
At the moment I store prices for products in the database as a pence number. So 4321 in the database means £43.21.
Then when reading it out, I divide by 100 to get it in pound and pence format.
However, I have a problem.
If the price is 4320, the returned value is 43.2 without the 0.
How can I get around this?
Thanks!
You can format strings with sprintf
See example 9:
<?php
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
?>
<?
echo money_format("%i", 1234.5)
//Output: 1234.50
?>
You can use money_format.
money_format() should do the trick. Alternatively number_format() or the powerful printf().
echo number_format($float, 2, '.', '');
and for pretty printing of large values:
echo number_format($float, 2, '.', ',');