Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert php strings to floating-point data

Shulou Source: shulou.com Published: 2022-06-02 21:15:14 09月16日 Update

This article focuses on "how to convert php strings into floating-point data". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to convert php strings into floating-point data.

Conversion method: 1, add the target type "(float)", "(double)" or "(real)" enclosed in parentheses before the string variable; 2, use the "floatval (string)" statement; 3, use the "settype (string variable," float ")" statement.

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

Convert php strings to floating-point data

Method 1: precede the string variable with the target type "(float)", "(double)" or "(real)" enclosed in parentheses

(float), (double), (real): convert target variables to floating-point types

Example:

Output result:

Method 2: use the floatval () function

Floatval (): used to get the floating-point value of a variable

Output result:

Method 3: use the settype () function

The settype () function sets the type of variable.

Syntax: settype (mixed & $var, string $type)

$type: the possible value of type is.

"boolean" (or "bool", starting with PHP 4.2.0)

"integer" (or "int", starting with PHP 4.2.0)

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

"string"

"array"

"object"

"null" (since PHP 4.2.0)

Example:

Output:

Note: the settype () function changes the original type of the variable, returning TRUE if the setting is successful and FALSE if it fails.

At this point, I believe you have a deeper understanding of "how to convert php strings into floating-point data". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Variables types characters strings floats methods data functions targets output content parentheses examples results statements useful learning practical deeper successful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB Xiaomi Apple Linux