Home  >  Article  >  Backend Development  >  How to convert binary to floating point number in php

How to convert binary to floating point number in php

青灯夜游
青灯夜游Original
2022-05-10 21:01:203020browse

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")".

How to convert binary to floating point number in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$str="11000110011";
$num=bindec($str);
echo "11000110011转为的十进制数是:".$num;
?>

How to convert binary to floating point number in php

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

How to convert binary to floating point number in php

  • settype() function

echo "<br>原数据类型为:";
var_dump($num);
settype($num, "float");
echo "修改后的类型:";
var_dump($num);

How to convert binary to floating point number in php

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!

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