This article will introduce to you how PHP converts scientific notation format into numeric strings. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Convert scientific notation format to string
Convert a number in scientific notation format: 1.2345678987654321e 30 to the string '123456789876543210000000000000'
First split the variables and reorganize to get the result
Implementation principle: (1) Split 1.2345678987654321e 30 into 1.2345678987654321 and 30
(2) Multiply 1.2345678987654321 by 10 The 30th power, 10 to the 30th power is obtained using the PHP function pow(10,30).
a. The first method, from the most common idea
public function sctonum($num){ if(false !== stripos($num, "e")){ $a = explode("e",strtolower($num)); $b = $a[0] * pow(10,$a[1]); return $b; }else{ return $num; } }
b. Use loops to get
public function numToStr($num) { $result = ""; if (stripos($num, 'e') === false) { return $num; } while ($num > 0) { $v = $num - floor($num / 10) * 10; $num = floor($num / 10); $result = $v . $result; } return $result; }
c. Use php functions to get all
public function sctonum($num, $double = 5){ if(false !== stripos($num, "e")){ $a = explode("e",strtolower($num)); return bcmul($a[0], bcpow(10, $a[1], $double), $double); }else{ return $num; } } //注$double 是小数位数,可默认设置为没有
Summary and suggestions: Ten thousand Harry Potters in the eyes of ten thousand people! Each result can be achieved by different processes. First, use the one you are most comfortable with, and then optimize it. Secondly, learn from methods that are understandable and efficient and standardized, such as c, which is simple and efficient.
Recommended learning:php video tutorial
The above is the detailed content of How to convert scientific notation format into numeric string in PHP. For more information, please follow other related articles on the PHP Chinese website!