PHP 8.5.0 Alpha 4 available for testing

Voting

: one plus zero?
(Example: nine)

The Note You're Voting On

depaula at unilogica dot com
8 years ago
As PHP doesn't have a a native number truncate function, this is my solution - a function that can be usefull if you need truncate instead round a number.

<?php
/**
* Truncate a float number, example: <code>truncate(-1.49999, 2); // returns -1.49
* truncate(.49999, 3); // returns 0.499
* </code>
* @param float $val Float number to be truncate
* @param int f Number of precision
* @return float
*/
function truncate($val, $f="0")
{
if((
$p = strpos($val, '.')) !== false) {
$val = floatval(substr($val, 0, $p + 1 + $f));
}
return
$val;
}
?>

Originally posted in http://stackoverflow.com/a/12710283/1596489

<< Back to user notes page

To Top