Home > Backend Development > PHP Problem > How to convert float to int type in php

How to convert float to int type in php

青灯夜游
Release: 2023-03-08 07:00:01
Original
5860 people have browsed it

How to convert float to int type in PHP: 1. Use forced type conversion and add the target type "(int)" enclosed in parentheses before the float variable to be converted, for example " (int)3.14"; 2. Use the intval() function to obtain the integer value of the variable.

How to convert float to int type in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php will convert float Into type int

#1. Forced type conversion--add the target type enclosed in parentheses before the variable to be converted

allowed The converted PHP data types are:

  • (int), (integer): converted to integer

  • (float), (double), (real): Convert to floating point type

  • (string): Convert to string

  • (bool), (boolean): Convert Into Boolean type

  • (array): Convert to array

  • (object): Convert to object

Example: Convert float type to int type

<?php   
$num1=3.14;   
$num2=(int)$num1;   
var_dump($num1); //输出float(3.14)   
var_dump($num2); //输出int(3)   
?>
Copy after login

Output:

How to convert float to int type in php

[Recommended learning: "PHP video tutorial 》]

2. Use the intval() function

<?php   
$num1=123.9;   
$num2=intval($num1); 
var_dump($num1); 
var_dump($num2); 
?>
Copy after login

Output:

How to convert float to int type in php

##intval( ) function is used to obtain the integer value of a variable.

intval() function returns the integer value of variable var by using the specified base conversion (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 "0x" ( or "0X"), use hexadecimal (hex); otherwise,

  • If the string starts with "0", use octal (octal); otherwise,

  • Decimal will be used.

Return value: The integer value of var is returned on success, and 0 is returned on failure. An empty array returns 0, a non-empty array returns 1.

Expand knowledge:


Generally, in this situation, unexpected price changes will occur in actual work. bug

If you have to pass a float price (2 decimal places)

and you need to convert it to int after doing the product (strange requirement)

intval(39.80*100) = 3979
intval(round(floatval($value) * 100)) = 3980
Copy after login

For more programming related knowledge, please visit:

Programming Video! !

The above is the detailed content of How to convert float to int type 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