I want to display price in my front page. But the format of the number required is,
Entered price: 10000 Display: 10,000
Entered price: 10000.10 Display: 10,000.1
Entered price: 10000.01 Display: 10,000.01
if I am using the following code
echo number_format ($price,2,'.',',');
But through this the result is displayed in this manner.
Entered price: 10000 Display: 10,000.00
Please help me to solve this problem
There's a function in PHP called money_format().
Have a look at it on http://php.net/manual/en/function.money-format.php
You have set number of decimial points to 2 so that is why you have 10,000.00. Try to user in this way:
echo number_format ($price,1,'.',',');
And also it is better to use money_format if you are working with money values.
Surely, from a clarity and consistency point of view, having 2 digits after the decimal point makes more sense especially when showing prices.
#barryhunter made a valid point and the following doesn't work.
echo rtrim(number_format($price,2,'.',','),'0.');
However, this does:
trim(trim(number_format($price,2,'.',','),'0'),'.');
Look:
<?php
$a=array('10000.00','10000.10','10000.01');
foreach ($a as $price)
{
echo $price.' - '.rtrim(rtrim(number_format($price,2,'.',','),'0'),'.')."\n";
}
?>
$> php -f t.php
10000.00 - 10,000
10000.10 - 10,000.1
10000.01 - 10,000.01
Personally I would do
echo number_format($price,floor($price)==$price?0:2,'.',',');
displaying a price as 10,000.1 just looks odd to me.
But if you really must
$bits = explode('.',$price);
echo number_format($price,strlen($bits[1]),'.',',');
(edit) In reply to the comment, it works for me...
<?php
$a=array(10000.00,10000.10,10000.01);
foreach ($a as $price)
{
$bits = explode('.',$price);
echo $price.' - '.number_format($price,strlen($bits[1]),'.',',')."\n";
}
?>
$ php t.php
10000 - 10,000
10000.1 - 10,000.1
10000.01 - 10,000.01
Related
I have an issue where I'm showing boxes of some posts from my database in a foreach loop.
The length of the post title affects the design. For example if one post has a long title next to one with a short title, it will push the link i have below down. This makes it look uneven.
Therefor I'm trying to write a function that checks if the length is too short, it should insert a line break.
This is what I have so far.
function insert_line_break($text){
if (strlen($text) < 10 ) {
echo "<br>";
}
}
<?= insert_line_break($entry["title"]) ?>
However that seems to replace the title with a linebreak.
What am I missing?
You have missed to echo the text itself.
function insert_line_break($text){
if (strlen($text) < 10 ) {
return "<br>";
}
}
<?php echo $entry["title"] . insert_line_break($entry["title"]); ?>
Note:
I changed the function to return a value instead of echoing it to the output, so it will be much more reusable in the future.
And I expanded the short tag <? to <?php and made the = to verbose echo which is much clearer and readable way of coding.
And of course I closed the line with semicolon ; as it should be.
EDIT:
<?php echo $entry["title"] . insert_line_break($entry["title"]); ?>
echo prints the content of $entry["title"] and then ask function insert_line_break($entry["title"]) to decide whether it contains string shorter than 10 characters, if so it returns <br> that is echoed. And that's it.
I'm working on a website
http://mtcdistrup.cluster003.ovh.net/
and I'm facing a problem. I'd like to display number of items in the cart over an image. I found a way for this, but now the problem is that I want to display it only if the number of item is not null.
I'm using Wordpress and woocomerce. So I found this to display number of item in the cart :
<?php echo sprintf ( _n( '%d', '<h6>%d<h6> ', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?>
and I want to use this in a kind of way, mix both to do my thing :
<?php
if (%d > 0)
{
echo "<h6>%d<h6>";
}
else
{
echo" ";
}
?>
I really have no idea about if I'm doing it correctly or not. So let me know how can I mix both of them in the best way.
Thanks a lot in advance !!!
In this case $d is not a variable but thing that says 'Hi, here will be an integer (read from arguments) displayed as a decimal number'. Then sprintf parse string and "replace" $d with WC()->cart->get_cart_contents_count(). Here you can read more about sprintf and formating.
In your case you need to use WC()->cart->get_cart_contents_count(). I don't know how would your code look like because I don't use wordpress. Anyway I think that information about what exactly is $d and information that you will use WC()->cart->get_cart_contents_count() should be enough to solve your problem.
HI just found a solution about add and sustract two columns here but I need to make the php code to format the numbers.
Here is the solution I got here:
<?php
while ($row= mysqli_fetch_assoc($result)) {
echo $row['money'] - $row['cost'];
}
?>
Its working fine for what I need but here is the thing. I use to run the follow php code to all money and numbers
<?php echo number_format($product['cost'],0, ",", "."); ?>
That way the numbers will display without decimals and like this 100.000
How can I make the code I found here to work with number format?
Something like this I tried but does not work
<?php
while ($row= mysqli_fetch_assoc($result)) {
echo number_format$row['money'] - number_format$row['cost'];
}
?>
It must substract and then show the result with formated numbers
you need to put it in ()
echo number_format($row['money'] - $row['cost'],0,',','.');
More than 2 :
echo number_format($row['money'] - $row['cost'] + $row['whatever'] * $row['whatever'],0,',','.');
I'm working on something that calculates me total weight of my class and average weight of my class. In $T I entered weight of girls in class, in
Echo "SUM OF WHOLE CLASS IS : $s";
must be SUM of weight of whole class and
echo "<br/>AVERAGE WEIGHT IS $p";
is average weight of class.. I dont see where the problem is, it just says that is on line number 4..
<body>
<?PHP
$T=array (47,47,62,60,71,55,50,52,62,80,65);
$s=0; $BUB=0;
for ($BUB=0;$BUB<=11;$BUB++)
{
$s=$s+$T[$BUB];
$BUB++;
}
$p=$s/11;
echo "SUM OF WHOLE CLASS IS : $s";
echo "<br/>AVERAGE WEIGHT IS $p";
?>
</body>
Make it simple using the array functions of PHP !
<?php
$T=array (47,47,62,60,71,55,50,52,62,80,65);
echo "SUM OF WHOLE CLASS IS : ".array_sum($T); //"prints the sum
echo "<br/>AVERAGE WEIGHT IS ".array_sum($T)/count($T); //"prints" the average
Demo
Try replacing that: $BUB<=11 with $BUB<11.
Notice that $BUB start with the value 0. as well as the array. so $T[11] isn't exists.
I have html code within a php function and it is displaying everything I need. I however find it very difficult to understand the use of . and ' when splitting up the code. I wish to change the layout to put the values within a table but struggle to get the correct syntax. Currently it looks like this;
$sub = $get_row['price']*$value; // Creates Subtotal of product
echo $get_row['name'].' x '.$value.' # £'.number_format($get_row['price'], 2).' = £ '.number_format($sub, 2).'
[-]
[+]
[Delete]<br/>';
Is there a good tutorial/example about how to easily style this? Can anyone guide me in how this should be styled?
I am looking to put it in the following;
<table>
<th>Name</th>
<th>Price</th>
<th>Subtotal</th>
.....
What I think you are looking for is something like this
while($row - mysqli_fetch_array($here is your query)){
$id = $row["id"];
$name = $row["name"];
$html .='
[-]
[+]
[Delete]<br/>';
}
?>
<html>
<?php echo $html;?>
In this it loops through your query and for each ID it finds it creates your links and appends it to the $html variable. Then when you echo they will display in order.
If you don't mind using quoteless keys (see Accessing arrays whitout quoting the key), I find that putting your variables inside of double quotes when possible makes the entire string more readable and reduces the amount of concatenation you need to do.
echo "$get_row[name] x $value # £".number_format($get_row['price'], 2)." = £ ".number_format($sub, 2);
If you want that even more readable, store the number_format calculations in variables first.
Also, for big strings of html, I like to close out php and use tags to echo the variables, because it's more readable to me (I prefer short echo tags (<?= ?>), but it's officially not encouraged so I can't officially recommend it here ;) ).
?>
[-]
[+]
[Delete]<br/>
<?