Show a number to two decimal places
You can use number_format():
return number_format((float)$number, 2, '.', '');
Example:
$foo = "105";
echo number_format((float)$foo, 2, '.', ''); // Outputs -> 105.00
This function returns a string.
PHP float with 2 decimal places: .00
A float doesn't have 0
or 0.00
: those are different string representations of the internal (IEEE754) binary format but the float is the same.
If you want to express your float as "0.00", you need to format it in a string, using number_format :
$numberAsString = number_format($numberAsFloat, 2);
php Determine a float variable has two decimal places
Everybody is dancing around the fact that floating point numbers don't have a number of decimal places in their internal representation. i.e. in float 100 == 100.0 == 100.00 == 100.000 and are all represented by the same number, effectively 100 and is stored that way.
The number of decimal places in this example only has a context when the number is represented as a string. In which case any string function that counts the number of digits trailing the decimal point could be used to check.
PHP: Round a number upto 2 decimal places and if the number is a whole number then add trailing zeros
You can try like this:
sprintf("%0.2f",$number);
Check sprintf
or try like this:
$rounded_value = number_format($number,2);
Check number_format
Remove trailing zeros until 2 decimals in PHP
You can use float casting
echo (float) 11.2200;
echo "<br/>";
echo (float) 11.2000;
echo "<br/>";
echo (float) 11.2340;
and you have to check number of digits after decimal point and than get value like below :
$val=(float) 11.2000;
if(strlen(substr(strrchr($val, "."), 1))<2){
echo number_format($val,2);
}
Casting a value into FLOAT in PHP loses the decimal points. Basically turning the value into an INT
Install proper locale settings on your machine. Probably you have installed IT (Italy) locale file and there is ,
as a decimal separator so casting doesn't "see" that you pass it float number.
setlocale(LC_ALL, 'en-US.utf8');
But it's not sufficient to type this line into your code. You must also install locale file.
More info: PHP setlocale has no effect
Round with 0 decimal point
round()
function omits last digit zero so better use the number_format() function.
eg:
round($No,2) //your code.
change this function to like below
eg:
number_format($No, 2);
I hope it will help you.
PHP7.1 json_encode() Float Issue
This drove me nuts for a bit until I finally found this bug which points you to this RFC which says
Currently
json_encode()
uses EG(precision) which is set to 14. That means that 14 digits at most are used for displaying (printing) the number. IEEE 754 double supports higher precision andserialize()
/var_export()
uses PG(serialize_precision) which set to 17 be default to be more precise. Sincejson_encode()
uses EG(precision),json_encode()
removes lower digits of fraction parts and destroys original value even if PHP's float could hold more precise float value.
And (emphasis mine)
This RFC proposes to introduce a new setting EG(precision)=-1 and PG(serialize_precision)=-1 that uses zend_dtoa()'s mode 0 which uses better algorigthm for rounding float numbers (-1 is used to indicate 0 mode).
In short, there's a new way to make PHP 7.1 json_encode
use the new and improved precision engine. In php.ini you need to change serialize_precision
to
serialize_precision = -1
You can verify it works with this command line
php -r '$price = ["price" => round("45.99", 2)]; echo json_encode($price);'
You should get
{"price":45.99}
Related Topics
What Does It Mean to Run PHP in Quiet Mode
Detect If Uploaded File Is Too Large
Simple Xml Add Namespaced Child
How to Find Timezone Id from Ip Address/Country Name in PHP
How to Get the File Path in HTML <Input Type="File"> in PHP
Data Transfer from JavaScript to PHP
How to Get Files in Ftp Folder Sorted by Modification Time
How to Do Url Rewriting in PHP
PHP Sending Variables to File_Get_Contents()
Are PHP Dateinterval Comparable Like Datetime