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

How to convert floating point number to integer in php

王林
Release: 2023-03-05 17:54:02
Original
3629 people have browsed it

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 ] )
Copy after login

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
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template