intval() / floatval() / number_format() Since: PHP 4(2000)
Converts values to integers or floating-point numbers, and formats numbers with digit separators. These are essential functions for type-casting strings to numbers and sanitizing user input.
Syntax
// Converts a value to an integer. An optional second argument specifies the base. intval($value, $base); // Converts a value to a floating-point number. floatval($value); // Formats a number with grouped thousands and optional decimal places. number_format($number, $decimals, $dec_point, $thousands_sep);
Function List
| Function | Description |
|---|---|
| intval($value, $base) | Converts a value to an integer. The optional second argument specifies the numeric base; if omitted, base 10 is used. Only the numeric portion at the start of a string is parsed. |
| floatval($value) | Converts a value to a floating-point number. Only the numeric portion at the start of a string is parsed; conversion stops at the first non-numeric character. |
| number_format($number, $decimals, $dec_point, $thousands_sep) | Returns a formatted string representation of a number. You can specify the number of decimal places, the decimal point character, and the thousands separator. |
Return Value
『intval()』returns an integer, and 『floatval()』returns a float. 『number_format()』returns a formatted string. If the value cannot be converted to a number, both 『intval()』and 『floatval()』return 『0』.
Sample Code
<?php
// Convert a string to an integer.
$str = "42 apples";
echo intval($str); // Parses the leading number and outputs 『42』.
// A string that does not start with a number becomes 『0』.
echo intval("apples42"); // Outputs 『0』.
// Convert from hexadecimal by specifying base 16.
echo intval("ff", 16); // Outputs 『255』.
echo intval("0b1010", 2); // Interpreted as binary and outputs 『10』.
// Use floatval() to convert a string containing a decimal.
$price = "1,280.50 dollars";
echo floatval($price); // Conversion stops at the comma and outputs 『1』.
echo floatval("3.14 is pi"); // Outputs 『3.14』.
// Use number_format() to add thousand separators.
$amount = 1234567.891;
echo number_format($amount); // No decimals, comma-separated — outputs 『1,234,568』.
echo number_format($amount, 2); // Two decimal places — outputs 『1,234,567.89』.
// Useful for displaying currency amounts.
$yen = 1500000;
echo number_format($yen) . " yen"; // Outputs 『1,500,000 yen』.
// Can be used to sanitize user input.
$_GET['page'] = "3; DROP TABLE users";
$page = intval($_GET['page']); // Only 『3』 is extracted.
Details
『intval()』converts a value of any type to an integer. It parses only the numeric portion at the start of a string and stops as soon as a non-numeric character is encountered. If the string does not start with a number, it returns 『0』, making it useful for sanitizing user input. Passing a base as the second argument also allows conversion from binary, octal, or hexadecimal strings.
『floatval()』works in the same way but converts to a floating-point number. Note that conversion stops at any comma in the string, so numeric strings containing thousand-separator commas will not parse as expected. To handle such strings, use 『str_replace()』to remove the commas before converting.
『number_format()』formats a number for display and is commonly used for currency and statistical output. Because it returns a string, you must convert the result back to a number if you need to perform further calculations.
If you find any errors or copyright issues, please contact us.