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, '.', ',');
Related
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
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);?>
I'll like to format 1000 to 10.00
The PHP number_format function does not seem to be working for this.
I have tried:
$amount2 = number_format("$cost",2,"",",");
echo "$cost";
Any ideas? Is there a way I can manupulate number_format to display the results (i.e just inserting a decimal before the last two digits?
Number format will change the "." to a "," but you telling it to format ONE THOUSAND.
$cost=1000;
echo number_format($cost,2,'.',',');
//1,000.00
What you want is simply:
$cost=1000;
echo number_format($cost/100,2,'.',',');
//10.00
Is this legit for you ?
<?php
$cost=1000;
echo substr($cost, 0, 2) . "." . substr($cost, 2);//10.00
1000 and 10.00 are totally different numbers (in values). Divide by 100, then format it properly:
$cost = 1000 ;
$cost /= 100 ;
$amount2 = number_format($cost,2,".","");
echo $amount2 ;
Try this code:
$stringA= 1000;
$length=strlen($stringA);
$temp1=substr($stringA,0,$length-2);
$temp2=substr($stringA,$length-2,$length);
echo $temp1.".".$temp2; // Displays 10.00
The third parameter to number_format should be the character you want to use as a decimal point. Why are you passing an empty string? And why are you placing your number ($cost) inside a string?
Try this: echo number_format($cost,2,'.',',');
EDIT: Perhaps I misunderstood your question — if you want the number 1000 to be displayed as 10.00, just divide $cost by 100 before calling number_format().
$price = 10.00;
list($dollars, $cents) = explode('.', $price);
echo $dollars . '.' . $cents;
... almost works except that the zeros are omitted. 10.00 becomes 10 and 10.10 becomes 10.1
I see there's a padding function for strings, but anything for numbers or floats?
How do I fix this?
You can use number_format:
echo number_format($price, 2); // Would print 10.00
You can specify a separator for the decimal point and another one for the thousands:
echo number_format(1234.56, 2, ',', ' '); // Would print 1 234,56
Use Sprintf
$digit = sprintf("%02d", $digit);
For more information, refer to the documentation of sprintf.
number_format is what you want: http://php.net/manual/en/function.number-format.php
Though i would recommend number_format, you could use
sprintf('%02.2f', $price)
if you want to rely on string functions.
Is there an easy way to echo a float number with a specific amount of digits after the decimal point?
For example: $sum = 3.1234566768; I would like to echo $sum and get: 3.12.
use number_format()
number_format($sum,2);
Try with:
$sum = 3.1234566768;
$rounded = round($sum, 2);
echo number_format($sum, 2); // 3.12
echo number_format((float)$ans, 4, '.', '');
I think this will work out