How do I truncate a decimal in PHP?
Yes intval
intval(1234.567);
intval(-1234.567);
No Truncate function in PHP - Options?
You can use PHP's floor function along with some decimal shifting like so:
floor(0.7666666667 * 100) / 100;
PHP dropping decimals without rounding up
You need floor() in this way:
$rounded = floor($float*100)/100;
Or you cast to integer:
$rounded = 0.01 * (int)($float*100);
This way it will not be rounding up.
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.
Can FLOAT types be truncated in MySQL stored functions?
Yes they are incompatible.
FLOAT stores the data in a IEEE-754 binary format which is stored in base 2. A base 10 fraction does not fit exactly. Thus your function gets something near 0.0012345 for the truncation but a FLOAT cannot hold that exact so you get the number required.
See the many answers here and elsewhere especially What Every Computer Scientist Should Know About Floating-Point Arithmetic for what a IEEE format floating point number is represented as.
If you want to have an exact number of decimal places you need to use the DECIMAL type
Delete digits after two decimal points, without rounding the value
TL;DR:
The PHP native function bcdiv seems to do precisely what is required, and properly.
To simply "truncate" a number, bcdiv($var, 1, 2);
where 2 is the number of decimals to preserve (and 1 is the denomenator - dividing the number by 1 allows you to simply truncate the original number to the desired decimal places)
Full Answer (for history)
This turns out to be more elusive than one might think.
After this answer was (incorrectly) upvoted quite a bit, it has come to my attention that even sprintf will round.
Rather than delete this answer, I'm turning it into a more robust explanation / discussion of each proposed solution.
number_format - Incorrect. (rounds)
Try using number format:
$var = number_format($var, 2, '.', ''); // Last two parameters are optional
echo $var;
// Outputs 2.50
If you want it to be a number, then simply type-cast to a float:
$var = (float)number_format($var, 2, '.', '');
Note: as has been pointed out in the comments, this does in fact round the number.
sprintf - incorrect. (sprintf also rounds)
If not rounding the number is important, then per the answer below, use sprintf:
$var = sprintf("%01.2f", $var);
floor - not quite! (floor rounds negative numbers)
floor, with some math, will come close to doing what you want:
floor(2.56789 * 100) / 100; // 2.56
Where 100 represents the precision you want. If you wanted it to three digits, then:
floor(2.56789 * 1000) / 1000; // 2.567
However, this has a problem with negative numbers. Negative numbers still get rounded, rather than truncated:
floor(-2.56789 * 100) / 100; // -2.57
"Old" Correct answer: function utilizing floor
So a fully robust solution requires a function:
function truncate_number( $number, $precision = 2) {
// Zero causes issues, and no need to truncate
if ( 0 == (int)$number ) {
return $number;
}
// Are we negative?
$negative = $number / abs($number);
// Cast the number to a positive to solve rounding
$number = abs($number);
// Calculate precision number for dividing / multiplying
$precision = pow(10, $precision);
// Run the math, re-applying the negative value to ensure returns correctly negative / positive
return floor( $number * $precision ) / $precision * $negative;
}
Results from the above function:
echo truncate_number(2.56789, 1); // 2.5
echo truncate_number(2.56789); // 2.56
echo truncate_number(2.56789, 3); // 2.567
echo truncate_number(-2.56789, 1); // -2.5
echo truncate_number(-2.56789); // -2.56
echo truncate_number(-2.56789, 3); // -2.567
New Correct Answer
Use the PHP native function bcdiv
echo bcdiv(2.56789, 1, 1); // 2.5
echo bcdiv(2.56789, 1, 2); // 2.56
echo bcdiv(2.56789, 1, 3); // 2.567
echo bcdiv(-2.56789, 1, 1); // -2.5
echo bcdiv(-2.56789, 1, 2); // -2.56
echo bcdiv(-2.56789, 1, 3); // -2.567
Related Topics
PHP Hierarchical Array - Parents and Childs
Remove All the Line Breaks from the HTML Source
How to Count Days Between Two Dates in PHP
Create a Dynamic MySQL Query Using PHP Variables
Understanding PHP & (Ampersand, Bitwise And) Operator
When to Pass-By-Reference in PHP
Executing a Shell Script from a PHP Script
Post and Get at the Same Time in PHP
Retrieve All Hashtags from a Tweet in a PHP Function
Regex Pattern to Get the Youtube Video Id from Any Youtube Url
Removing Last Comma from a Foreach Loop