mcfc4heatons Posted August 23, 2022 Share Posted August 23, 2022 (edited) I've seen this in some PHP source code: number_format((float)$thenumber, 2, '.', ''); What is the difference between that and this?: number_format($thenumber, 2, '.', ''); thanks... Edited August 23, 2022 by mcfc4heatons typo Quote Link to comment https://forums.phpfreaks.com/topic/315225-php-float-and-number_format/ Share on other sites More sharing options...
ginerjm Posted August 23, 2022 Share Posted August 23, 2022 (edited) Good question. Here's what I did: $thenumber = 12; echo "$thenumber with float: ".number_format((float)$thenumber, 2, '.', '').'<br>'; echo "$thenumber without float: ".number_format($thenumber, 2, '.', '').'<br>'; $thenumber = 5.2; echo "$thenumber with float: ".number_format((float)$thenumber, 2, '.', '').'<br>'; echo "$thenumber without float: ".number_format($thenumber, 2, '.', '').'<br>'; Results: 12 with float: 12.00 12 without float: 12.00 5.2 with float: 5.20 5.2 without float: 5.20 Easy answer. Doesn't seem to matter. Edited August 23, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/315225-php-float-and-number_format/#findComment-1599706 Share on other sites More sharing options...
Barand Posted August 23, 2022 Share Posted August 23, 2022 (float) is casting $thenumber as a float. But number_format() function definition (from the manual) is number_format( float $num, int $decimals = 0, string|null $decimal_separator = ".", string|null $thousands_separator = ",") : string therefore $thenumber is coerced to float type anyway. So, as demonstrated above, it makes no difference. Quote Link to comment https://forums.phpfreaks.com/topic/315225-php-float-and-number_format/#findComment-1599710 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.