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, '.', '');
Related
I have a number in php formatted as 5299, and I'd like for it to be converted to a number that's decimal formatted as 52.99 to resemble money. I've messed around with php's number_format() but it doesn't seem like there is a way to add decimals for converting to cents. This number is dynamically calculated, so the 5299 might be 12122, where 5299 would be 52.99 and 12122 would be converted to 121.22.
Just use a simple function like this.
<?php
$value = 50;
echo number_format(($value /100), 2, '.', ' ');
?>
I have a string ($maxDeposit) which is a numeric monetary value. So, for example:
123.00
This string is being passed in to jQuery, it needs to be passed in as a numeric data type. I'm achieving this using the following:
$maxDeposit = floatval($maxDeposit);
This loses the last last decimal place however, so my number looks like:
123.0
I have this method of converting the number to two decimal places:
$maxDeposit = sprintf('%0.2f', round($maxDeposit, 2));
However this also converts the number back to a string. Is there a way I can convert the string to a float but keep the last decimal place? Thanks
No, float is a numeric value, and 123.00 is its representation with 2 decimal places. It is responsibility of view layer to format numbers. In your case it is jQuery, e.g. console.log(maxDeposit.toFixed(2)).
I think, You can use floatval/float and number_format.
$maxDeposit = number_format(floatval($maxDeposit), 2);
or
number_format((float)$maxDeposit, 2, '.', '');
http://php.net/manual/pt_BR/function.number-format.php
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
I've a simple question.
I've this 7310093341976450848 number which I needed to echo.But when I echo it gives me this 7.3100933419765E+18.
I tried echo (string)$data; to cast it to string and then print it but it's still giving the same result.
The number is initially of type double.
I've this 7310093341976450848 number which I needed to echo.
The number is initially of type double.
Because of the floating point representation used in PHP, once it's stored as a double, you cannot print out that exact number anymore.
This is one of the (many) ways to print out the value:
printf("%.0f", $data);
echo number_format($data, 0, '', '');
If you don't want to lose precision, store it as a string, or use one of the arbitrary precision libraries: BC Math / GMP.
Use number_format(). Here's how:
echo number_format( $data, 0, '', '' );
I have a number stored in a variable. The number is pulled from the database as 9900..I need to convert this number to 99.00 in order to display to a customer in HTML.
I was wondering how can I achieve this in php.
Thanks.
$priceInCents = 9900;
$priceInDollars = $priceInCents / 100;
And then you can use round() or number_format() as you please.
You can use the number_format() function for that:
echo number_format(9900/100);
You can do this by either using money_format or number_format, if you are going to display it as a price you should look at money_format, otherwise, number_format is the way to go.