If I have a.toFixed(3);
in javascript ('a' being equal to 2.4232) what is the exact equivalent command in php to retrieve that? I searched for it but found no proper explanation appended to the answers.
Asked
Active
Viewed 3.5k times
23

user111671
- 421
- 2
- 5
- 14
-
1I think PHPs `round()` is going to be as close as you get out of the box...http://php.net/round – DrCord Dec 18 '13 at 22:51
-
1Aside from `sprintf` `number_format` is the closest. – AbraCadaver Dec 18 '13 at 22:56
8 Answers
44
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

Case
- 4,244
- 5
- 35
- 53

cassiodoroVicinetti
- 596
- 1
- 5
- 4
-
Note: `number_format` returns string not float or int which could make some errors in some cases. – Sameh Ashraf Jun 14 '23 at 10:25
26
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

E_net4
- 27,810
- 13
- 101
- 139

Shafiq Jetha
- 1,337
- 16
- 30
-
4In case of using it for round(2.4255, 2) it would return 2.43 – Dănuț Mihai Florian Jan 04 '14 at 00:12
-
@DănuțMihaiFlorian It depends on what you've set as `mode` http://php.net/manual/en/function.round.php#refsect1-function.round-parameters – yckart Aug 02 '16 at 12:28
-
3
-
1@NalinAgrawal: You may have to cast it to a double to get that, though it seems that you might be wanting that for displaying on a page, in which case there are string formatters that will maintain certain decimal precision, though that's more string-related than numerical-related. – Shafiq Jetha Jan 16 '17 at 16:39
11
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

Stephan Stanisic
- 815
- 7
- 15

Dănuț Mihai Florian
- 3,075
- 3
- 26
- 44
4
A direct equivalent is sprintf('%.03F', $a)
. This will format the value in question as a number with 3 decimal digits. It will also round if required.

Jon
- 428,835
- 81
- 738
- 806
2
The straight forward solution is to use in php is number_format()
number_format(2.4232, 3);

Afia
- 683
- 5
- 17
-
1How is this different from @cassiodoroVicinetti's answer from Aug 28 '14 at 19:15 (4 1/2 **years** ago)? – Stephen P Mar 27 '19 at 19:55
-
1
1
Here is the translation:
function toFixed($number, $dec_length){
$pos=strpos($number.'', ".");
if($pos>0){
$int_str=substr($number,0,$pos);
$dec_str=substr($number, $pos+1);
if(strlen($dec_str)>$dec_length){
return $int_str.($dec_length>0?'.':'').substr($dec_str, 0,$dec_length);
}else{
return $number;
}
}else{
return $number;
}
}
//toFixed(1234.678, 0) = 1234
//toFixed(1234.678, 1) = 1234.6
//toFixed(1234.678, 2) = 1234.67
//toFixed(1234.678, 4) = 1234.678

dev4life
- 10,785
- 6
- 60
- 73
0
how about this
function to_fixed($number, $decimals) {
return floatval(number_format($number, $decimals, '.', ""));
}
call it like this
to_fixed(2.4232, 3); // 2.423

user889030
- 4,353
- 3
- 48
- 51