Home >Backend Development >PHP Problem >How to convert binary to floating point number in php
Method: 1. Use bindec() to convert binary to decimal number, syntax "bindec (binary value)"; 2. Use type conversion function floatval() or settype() to convert decimal number to floating point Type, syntax "floatval(number)" or "settype(number, "float")".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php will convert binary Method for floating-point number
In PHP, if you want to convert binary to floating-point number, you need to convert binary to decimal number first.
Implementation method:
1. Use the bindec() function to convert binary numbers into decimal numbers
bindec($binary_string)
Function converts the binary number $binary_string to a decimal number.
<?php header('content-type:text/html;charset=utf-8'); $str="11000110011"; $num=bindec($str); echo "11000110011转为的十进制数是:".$num; ?>
2. Use the type conversion function floatval() or settype() to convert the decimal number to a floating point type.
floatval() function
The floatval() function is a function that specifically converts other types to floating point types. You can get the floating point value of a variable.
echo "<br>十进制数的数据类型为:"; var_dump($num); $float= floatval($num); echo "修改后的类型:"; var_dump($float);
settype() function
echo "<br>原数据类型为:"; var_dump($num); settype($num, "float"); echo "修改后的类型:"; var_dump($num);
Recommended learning : "PHP Video Tutorial"
The above is the detailed content of How to convert binary to floating point number in php. For more information, please follow other related articles on the PHP Chinese website!