search
HomeBackend DevelopmentPHP ProblemHow to convert floating point type to int type in php

How to convert floating point type to int type in php

Jan 10, 2022 pm 06:03 PM
floatintphpData type conversionfloating point

How to convert floating point type to int type in php: 1. Use intval() function, syntax "intval (floating point data)"; 2. Use settype() function, syntax "settype( Floating point data, 'integer')".

How to convert floating point type to int type in php

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

php will float Method to convert point type to int type:

Method 1: Use intval() function

intval() function is used to obtain the variable Integer value.

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 ] )

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.

Example: Convert floating point type to int type

<?php   
$num1=3.1415926;   
$num2=intval($num1); 
var_dump($num1); 
var_dump($num2); 
?>

How to convert floating point type to int type in php

##Method 2: Use settype() function

settype() function: used to set the type of variables.

The type that can be set is:

  • "boolean" (or "bool", starting from PHP 4.2.0)

  • "integer" (or "int" since PHP 4.2.0)

  • "float" (only available since PHP 4.2.0, for older versions "double" used in is now deprecated)

  • "string"

  • "array"

  • "object"

  • "null" (from PHP 4.2.0)

Note: The settype() function will change the variable type itself.

Example: Convert floating point type to int type

<?php
header("Content-type:text/html;charset=utf-8");
$num1=2.24;
$num2=52.36;
echo &#39;原 $num1 和 $num1:<br>&#39;;
var_dump($num1); 
var_dump($num2); 

settype($num1, &#39;integer&#39;);
settype($num2, &#39;integer&#39;);
echo &#39;<br>现 $num1 和 $num1:<br>&#39;;
var_dump($num1); 
var_dump($num2); 
?>

How to convert floating point type to int type in php

Recommended learning: "

PHP Video Tutorial"

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.