可以将指数或科学计数法表示的数字转换为十进制表示吗?

huangapple go评论57阅读模式
英文:

Can we convert exponential or scientific notation number to decimal notation?

问题

I have a number 3.43E-6 and I need to convert it into 0.00000343. Is that possible with PHP?

我有一个数 3.43E-6,我需要将它转换为 0.00000343。这在PHP中是否可能?

I tried PHP number_format and also tried using the answer of this post (float), but it does not work as expected.

我尝试使用PHP的number_format,还尝试使用此帖子中的答案(float),但它不像预期的那样工作。

The mentioned number is not a string but a decimal value, and by the answer mentioned in that post we can get the normal whole number from positive exponential values (5.78e5, 3.14e5), but not working for the negative exponential values like 3.43E-6, 6.24464e-7, 5.864e-5. It returns the same value that we give as an input if we use a negative exponential value.

上述提到的数字不是字符串,而是十进制值,而根据该帖子中提到的答案,我们可以从正指数值(5.78e5, 3.14e5)获取正常的整数,但对于负指数值(如3.43E-6, 6.24464e-7, 5.864e-5),它不起作用。如果我们使用负指数值作为输入,它会返回与输入相同的值。

Number: 3.43E-6

数字:3.43E-6

Expected result: 0.00000343

预期结果:0.00000343

英文:

I have a number 3.43E-6 and I need to convert it into 0.00000343. Is that possible with PHP?

I tried PHP number_format and also tried using the answer of this post (float), but it does not work as expected. The mentioned number is not a string but a decimal value, and by the answer mentioned in that post we can get the normal whole number from positive exponential values (5.78e5, 3.14e5), but not working for the negative exponential values like 3.43E-6, 6.24464e-7, 5.864e-5. It returns the same value that we give as an input if we use a negative exponential value.

Number: 3.43E-6

Expected result: 0.00000343

答案1

得分: 1

$number = 3.43E-6;
$decimal = sprintf('%.8f', $number);
echo '$' . $decimal; // output: $0.00000343

also this is another example

$decimal = sprintf('%.10f', $number);
echo '$' . $decimal; // output: $0.0000034300

there is other way of doing this:

$converted_number = number_format($number, 8, '.', '');
echo $converted_number; // Output: 0.00000343

英文:

yes it is possible try this and you can modify this 8 if u want more decimal '%.8f

$number = 3.43E-6;
$decimal = sprintf('%.8f', $number);
echo '$' . $decimal; // output: $0.00000343

also this is another example

$decimal = sprintf('%.10f', $number);
echo '$' . $decimal; // output: $0.0000034300

there is other way of doing this :

$converted_number = number_format($number, 8, '.', '');
echo $converted_number; // Output: 0.00000343

答案2

得分: 1

使用提供的答案,我创建了一个类似下面这样的函数,似乎工作正常。

function convertExponentialToDecimal($number) {

  $full_string = $number;
  $num_string_without_dollar = str_replace('$', '', $full_string);

  $is_exponential = preg_match('/^[+-]?\d+(\.\d+)?[Ee][+-]?\d+$/i', $num_string_without_dollar); // 检查数字是否处于指数表示法中
  if ($is_exponential) {
    
      $decimal = sprintf("%.20f", $num_string_without_dollar);
      $decimal = bcmul($decimal, '1', 20);
      $decimal = rtrim($decimal, '0');
      $decimal = rtrim($decimal, '.');

  } else {
      $decimal = (float)$num_string_without_dollar;
  }

  return $decimal;

}

此函数可以将指数值转换为整数(以十进制表示)。

echo convertExponentialToDecimal('6.20503e-7');

上述代码的结果是 0.000000620503

我不确定这是否是正确的解决方案,但它确实可以正常工作。

谢谢大家!

英文:

Using the answers provided, I created a function like the one below, which seems to be working fine.

function convertExponentialToDecimal($number) {

  $full_string = $number;
  $num_string_without_dollar = str_replace('$', '', $full_string);

  $is_exponential = preg_match('/^$?[+-]?\d+(\.\d+)?[Ee][+-]?\d+$/i', $num_string_without_dollar); // check if the number is in exponential notation
  if ($is_exponential) {
    
      $decimal = sprintf("%.20f", $num_string_without_dollar);
      $decimal = bcmul($decimal, '1', 20);
      $decimal = rtrim($decimal, '0');
      $decimal = rtrim($decimal, '.');

  } else {
      $decimal = (float)$num_string_without_dollar;
  }

  return $decimal;

}

This function can convert the exponential values to a whole number (in decimal notation).

echo convertExponentialToDecimal('6.20503e-7');

The above code gives a result of 0.000000620503.

I don't know if this is the proper solution or not; however, it is working correctly.

Thank you all!

huangapple
  • 本文由 发表于 2023年3月31日 19:42:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定