search
HomeBackend DevelopmentPHP ProblemHow to convert php string to floating point type

3 conversion methods: 1. Add the target type "(float)", "(double)" or "(real)" enclosed in parentheses before the string to be converted, the syntax is "( float) string". 2. Use the floatval() function to obtain the floating point value of a string variable, with the syntax "floatval (string variable)". 3. Use the settype() function to set a string variable to a floating point type, with the syntax "settype(string variable, "float")".

How to convert php string to floating point type

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php string conversion Three methods for floating point numbers

Method 1. Add the target type enclosed in parentheses before the string to be converted

The target types are:

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

Example: Convert string type Convert to float type

<?php   
$str="23.25";   
$float=(float)$str;   
var_dump($str); 
var_dump($float); 
?>

How to convert php string to floating point type

Method 2. Use floatval() function--floating point conversion function

floatval — Get Floating point value of variable

Example:

<?php   
$str="3.14";   
$float=floatval($str);   
var_dump($str); 
var_dump($float); 
?>

How to convert php string to floating point type

Method 3: Use settype() function--Type setting function

settype(): used to set a variable to a specified type (the settype() function will change the original type of the variable).

settype ( $var , $type )
  • $var: The variable to be converted.

  • #$type: The target type to which the variable is to be converted.

Return value: TRUE when the setting is successful, FALSE when the setting fails.

Just set the second parameter $type value of the settype() function to "float".

<?php   
$str="5.14";  
var_dump($str);  
$float=settype($str,"float");   
var_dump($str); 
var_dump($float);//返回值
?>

How to convert php string to floating point type

Explanation: The value of the second parameter (set type) of the settype() function can be:

  • "boolean " (or "bool" from PHP 4.2.0 onwards)

  • "integer" (or "int" from PHP 4.2.0 onwards)

  • "float" (only available after PHP 4.2.0, "double" used in older versions is now disabled)

  • "string"

  • "array"

  • "object"

  • "null" (from PHP 4.2.0 Starting)

Recommended study: "PHP Video Tutorial"

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

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools