本文介绍了在 PHP 中将完整字符串(以指数格式表示)转换回数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找将指数数字字符串转换为精确数字的方法.所以,我在 MySQL 中以字符串形式存储了一个指数数字.我想以确切的数字转换回这个字符串.但是这个数字好像很大,越界了,给我提供了错误的数据.

I've been searching for converting my exponential number string into an exact number.So, I've an exponential number stored as string in MySQL. I want to convert back this string in the exact number.But It seems the number is quite big and crosses the boundary and provide me wrong data.

虽然我尝试以下各种格式的代码,但结果格式不正确.

While I tried following code in various format but result is not in proper format.

policy_number = "2.9992020830803E+18";
number_format($policy_number, 0,'.','');
// Output
 2999202083080300032


(float) $policy_number;
// Output
2.9992020830803E+18


sscanf($policy_number, "%f")[0];
// Output
2.9992020830803E+18


floatval($policy_number);
// Output
2.9992020830803E+18


gmp_init("2.9992020830803E+18");
gmp_strval($policy_number);
// Output
0


"2.9992020830803E+18" + 0;
// Output
2.9992020830803E+18

请告诉我正确的转换方法.

Please show me the right way to convert it.

推荐答案

更新

我同意 Ed Cottrell 的观点,即您的问题是如何将数据存储在数据库中.但是,如果这是一个您已经在查看已存储为指数的大量数据的项目,那么我编写的这个函数应该适合您.它应该适用于正负底数和指数.它基本上模仿了您手动操作的方式.

I agree with Ed Cottrell that your problem is how you are storing your data in the db. However, if this is a project where you are already looking at a large set of data that is already stored as an exponent then this function I wrote should work for you. It should work for positive and negative bases and exponents. It basically mimics the way you would do the operation by hand.

我无法找到使用数学函数的方法.如果有人知道如何做得更好,请发布.与此同时,我写这篇文章很开心.

I was not able to figure out a way to do it using math functions. If someone knows how to do it better please post. In the meantime, I had fun writing this.

希望能帮到你!

function getRealNumber($number){

  //Parse the base and exponent.
  preg_match('/^(.*?)E[\-|\+](.*?)$/', $number, $data);

  $base = $data[1];
  $exp = $data[2];

  //Test to see if the base is negative.
  if(preg_match('/\-/', $base)){

    $base = str_replace('-', '', $base);
    $isNegative = TRUE;

  }

  //Capture the offset of the decimal point.
  preg_match('/\./', $base, $position, PREG_OFFSET_CAPTURE);

  $offset = $position[0][1]; //This is the offset of the decimal point.
  $string = str_replace('.', '', $base); //Get numbers without decimal.
  $length = strlen($string); //Get the length of string.

  //Test to see if we are adding zeros to the end or beginning of string.
  if(preg_match('/E\+/', $number)){

    //Let's move the decimal.
    if($length > ($exp + $offset)){

      $string = substr_replace($string, '.', ($exp + $offset), 0);

    } else {

      $string = $string . str_repeat('0', $exp - ($length - $offset));

    }


  }elseif(preg_match('/E\-/', $number)){

    //Calculate the number of zeros needed to add and append them.
    if($offset > $exp){

      $string = substr_replace($string, '.', $offset, 0);

    } else {

      $string = '0.' . str_repeat('0', $exp - $offset) . $string;

    }

  }

  //Add the negative sign if we need to.
  if(!$isNegative){

    return $string;

  } else {

    return '-' . $string;

  }

}

$policy_number = "2.9992020830803E+18";
echo getRealNumber($policy_number);

//Will output 2999202083080300000

这篇关于在 PHP 中将完整字符串(以指数格式表示)转换回数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:58