What is the exact equivalent of JS: something.toFixed() in PHP?

I found that sprintf and number_format both round the number, so i used this:

$number = 2.4232;
$decimals = 3;
$expo = pow(10,$decimals);
$number = intval($number*$expo)/$expo; //  = 2423/100

The exact equivalent command in PHP is function number_format:

number_format($a, 3, '.', ""); // 2.423
  • it rounds the number to the third decimal place
  • it fills with '0' characters if needed to always have three decimal digits

Here is a practical function:

function toFixed($number, $decimals) {
  return number_format($number, $decimals, '.', "");
}

toFixed($a, 3); // 2.423

Have you tried this:

round(2.4232, 2);

This would give you an answer of 2.42.

More information can be found here: http://php.net/manual/en/function.round.php

Tags:

Javascript

Php