How to convert scientific notation format into numeric string in PHP

醉折花枝作酒筹
Release: 2023-03-09 17:08:01
forward
2152 people have browsed it

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.

How to convert scientific notation format into numeric string in PHP

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;
   }
}
Copy after login

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;
}
Copy after login

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 是小数位数,可默认设置为没有
Copy after login

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!

Related labels:
php
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!