I am adding 2 prices together (which are session variables) in php and I want it to show 2 decimal places. The session variables themselves show as 2 decimal places but when added together and for example the result is 2.50 only 2.5 is displayed. Is their a way I can display the two decimal places? This is the code I am using
<div id="info">
<span class="bluetext2">Total: </span>$<?php echo $_SESSION['startingPrice'] + $_SESSION['postage']; ?><br>
</div>
You have a couple of options here.
number_format - will output the decimal places, can also be used to specify a decimal point character as well as a thousands separator.
echo number_format($x, 2);
printf/sprintf - These are identical except that printf output whereas sprintf returns
printf('%.2f', $x);
echo sprintf('%.2f', $x);
money_format - Locale aware money formater, will use the proper decimal and thousands separators based on locale.
setlocale(LC_MONETARY, "en_US");
echo money_format("%i", $x);
You can try this :
$Total = number_format((float)$number, 2, '.', '');
use echo number_format("2.5",2);
Use number_format function. This code:
echo number_format(12345.1, 2, ".","");
will give result: 12345.10
Also you can use short version:
number_format(12345.1, 2)
which results in 12,345.10 (I think that is english format ... )
As simple as -
if u store that as an integer
like $total=5.5000 at the time of displaying it will display 5.5.
If u use is as $total="5.5000" then it will display as 5.5000
OR
$asdf=$x+$y; //5=2.50+2.50
echo number_format($asdf,2);
Possible duplicate to
PHP: show a number to 2 decimal places
Use number_format((float('number to be rounded off'),' number of decimal places to be rounded off','separator between the whole number and the number after the separator')
example
$foo = 150
echo number_format(float($foo),2,'.')
will give 150.00
Related
I set int data type in mysql database but it not showing decimal place like 10.20
Then I set decimal(10,5) it show five decimal place with all number thought it not necessary like 10.00000.
I want to show decimal places only if have fraction
like 10.25455 = 10.25455 and 10 = 10 but it showing 10 = 10.00000
how to solve this
You can use PHP round function.
http://php.net/manual/en/function.round.php
PHP Code:
<?php
echo round(10.25455, 5); // outputs: 10.25455
echo '<br/>';
echo round(10.00000, 5); // outputs: 10
Just add 0!
echo 10.25455 + 0;
echo 10.00000 + 0;
Output:
10.25455
10
Use decimal as a field type. Then in PHP use round(); function.
There is no datatype in mysql that supports your requirement. In fact, that is not how computers work. A number is either an int or a float.
You might want to use varchar instead, and cast to the right data type in php, but it might cause performance overhead. It is indeed a weird data type requirement. If you must go this route, consider adding an extra column for flagging data type.
echo floatval($value);
will do the trick.
I have a question regarding number formating in PHP.
I have a variable called "average", which is simply an average of a few values. To make it clear I rounded the number to 2 decimal places. Now the problem is, that if the average is for example 2.90, it only shows 2.9. Is there any way of displaying 2 decimal places always? I though I could do it by multiplying the number by 100, rounding it to zero d.p. and then divide by 100 again, but that seems a bit overcomplicated if there is an easier way of doing it.
Maybe you can try the number_format(float $number [, int $decimals = 0 ])?
For more information, take a look at http://php.net/manual/en/function.number-format.php
Format the output with printf
printf("%.1f", $num); // prints 1 decimal place
printf("%.2f", $num); // prints 2 decimal places
I need to display a floating point number with a specified number of decimal places (rounding up), specifically up to two decimal places even though the number has no fractional part. One way that I know is by using the sprintf() PHP function as follows.
echo sprintf("%0.2f", 123);
It returns 123.00.
echo sprintf("%0.2f", 123.4555);
returns 123.46 but the following doesn't return what I need.
echo sprintf("%0.2f", 123.455); // 5 is removed - the right-most digit.
I expect it to return 123.46 but it doesn't. It returns 123.45. Although it's not a huge difference, I somehow need to return 123.46 in both of the cases.
The round() function can do it. echo round(123.455, 2); and echo round(123.4555, 2); return 123.46 in both the cases but I can't think of using this function because if the fractional part is not present, it displays no decimal digits at all. like echo round(123, 2); gives 123 and I need 123.00.
Is there a function in PHP to achieve this?
number_format(123.455, 2, '.', '')
I want to format a float value I have to two decimal places.
For example if I have a float for a price of 5.2, I want it to be formatted as 5.20.
Try number_format:
echo number_format("5.2",2)
You'll want to use printf or sprintf. You can read more about it in the php docs.
If you're simply trying to ensure a number is displayed with two decimal places, you could also use number_format, or (if this is a currency value) money_format.
e.g.: echo number_format('5.2', 2);
Try this:
number_format((float)$foo, 2, '.', '');
Is there a PHP function that will make the number always have 2 decimals places, even if it's 0?
No, but you can format a number to a string with decimals
number_format — Format a number with grouped thousands
Example:
echo number_format(0, 2); // 0.00
EDIT: the printf/sprintf solutions suggested deserve some upvotes too
Do you mean you want to print additional digits even if there's only one digit after the decimal? You can do something like:
$x = 0.1;
printf("%.2f", $x);