Home  >  Article  >  Backend Development  >  How to convert variables to float in php

How to convert variables to float in php

青灯夜游
青灯夜游Original
2021-06-07 18:00:162842browse

Method: 1. Add "(float)" before the variable or value, for example "(float)123"; 2. Use the floatval() function, the syntax is "floatval(variable)"; 3. Use " "operator, the syntax is "variable 0.0"; 4. Use the round() function, the syntax is "round(variable, 2)".

How to convert variables to float in php

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

PHP will change a variable Several methods of converting to float

First of all, PHP is the best language. There are many methods for data types. Below are examples one by one. If there are any deficiencies, please feel free to add

$str = 'NaN';
// 强制类型转换----float只能转换前面带数字的字符串
echo (float) $str;
echo &#39;<br/>&#39;;

//floatval() — 获取变量的浮点值
echo floatval($str);
echo &#39;<br/>&#39;;

// 做一个加法运算也可以达到效果
echo ($str + 0.0);
echo &#39;<br/>&#39;;

// round四舍五入,返回了0,但并没有保留2位小数
echo round($str,2);
echo &#39;<br/>&#39;;

Explanation:

1. Forced type conversion--when you want to convert The variable is preceded by the target type enclosed in parentheses

  • . The PHP data types allowed for conversion are:

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

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

  • (string): Convert to string

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

  • ( array): Convert to array

  • (object): Convert to object

2, floatval()

floatval - Get the floating point value of the variable

Syntax:

floatval ( mixed $var ) : float

Return the float value of the variable var.

Parameters: var can be any scalar type. You cannot use floatval() with arrays or objects.

3. round()

The round() function rounds floating point numbers.

Syntax

round(number,precision,mode);

ParametersDescriptionRequired. Specifies the value to be rounded. Optional. Specifies the number of digits after the decimal point. Default is 0, can also be negative. Optional. Specifies a constant representing the rounding mode:
number
precision
mode
    PHP_ROUND_HALF_UP - Default. Round up
  • number to precision decimal places when encountering .5. Rounds 1.5 to 2 and -1.5 to -2.
  • PHP_ROUND_HALF_DOWN - Round down
  • number to precision decimal places when encountering .5. Rounds 1.5 to 1 and -1.5 to -1.
  • PHP_ROUND_HALF_EVEN - When encountering .5, take the next even value and round
  • number to precision decimal places.
  • PHP_ROUND_HALF_ODD - Rounds the next odd value when encountering .5
  • number to precision decimal places.
Recommended study: "

PHP Video Tutorial"

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