Convert long to int in PHP
Using
number_format()
Function. The number_format()
function is used to convert string into a number. It returns the formatted number on success otherwise it gives E_WARNING on failure.<?php
$num = "1000.314";
// Convert string in number using
// number_format(), function
echo number_format($num), "\n";
// Convert string in number using
// number_format(), function
echo number_format($num, 2);
?>
Output
1,000
1,000.31
1,000.31
Using type casting: Typecasting can directly convert a string into float, double or integer primitive type. This is the best way to convert a string into number without any function.
<?php
// Number in string format
$num = "1000.314";
// Type cast using int
echo (int)$num, "\n";
// Type cast using float
echo (float)$num, "\n";
// Type cast using double
echo (double)$num;
?>
Output
1000
1000.314
1000.314
1000.314
1000.314