Home  >  Article  >  Backend Development  >  How to convert floating point number to integer in php

How to convert floating point number to integer in php

王林
王林Original
2020-09-03 15:41:113568browse

php method to convert floating point numbers to integers: You can use PHP's built-in function [intval()] to achieve this. This function is used to obtain the integer value of the variable. It returns the interger value when successful and returns 0 when it fails. The syntax is [int intval(mixed $var[,int $base=10])].

How to convert floating point number to integer in php

(Recommended tutorial: php video tutorial)

php method to convert floating point numbers to integers :

Built-in function intval() in php is used to get the integer value of a variable. Returns the integer value of var on success and 0 on failure. An empty array returns 0, a non-empty array returns 1.

intval()The function returns the integer value of the variable var by using the specified base conversion (the default is decimal). intval() cannot be used with object, otherwise an E_NOTICE error will be generated and 1 will be returned.

Syntax:

int intval ( mixed $var [, int $base = 10 ] )

Parameter description:

  • $var: The quantity value to be converted to integer.

  • #$base: The base used for conversion.

If base is 0, the base used is determined by detecting the format of var: if the string includes the prefix of "0x" (or "0X"), use hexadecimal (hex); otherwise, if the string starts with "0", octal will be used; otherwise, decimal will be used.

(Related recommendations: php training)

Code sample:

<?php
echo intval(42);
// 42
echo intval(4.2);
// 4
echo intval(&#39;42&#39;);
// 42
echo intval(&#39;+42&#39;);
// 42
echo intval(&#39;-42&#39;);
// -42
echo intval(042);
// 34
echo intval(&#39;042&#39;);
// 42
?>

The above is the detailed content of How to convert floating point number to integer in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn