简体   繁体   English

PHP编号:将1.4EN(N> 40)转换为十进制,删除无用的零位数

[英]PHP number : convert 1.4E-N (N >40) to decimal with useless zero digits removed

I am getting very small values from a third party service, I need it to convert to number with useless digits removed. 我从第三方服务获得非常小的值,我需要它转换为删除无用数字的数字。 ` `

<?php 
    $decimal = 50 
    //If value is 1.4E-45
    number_format($value, $decimal); 
    //output: 0.00000000000000000000000000000000000000000000140000 
    number_format($value, $decimal) +0 ;
    //output: 1.4E-45 
?>

` `

Sample Input :: 样本输入::

  • 1 1

    output 1 输出1

  • 1.00000000000000000000000000000000000000000000000000 1.00000000000000000000000000000000000000000000000000

    output : 1 输出:1

  • 1.4E-45 1.4E-45

    output : 0.0000000000000000000000000000000000000000000014 输出:0.0000000000000000000000000000000000000000000014

As you can see in code, already tried out to add 0 and number_format method. 正如您在代码中看到的,已经尝试过添加0和number_format方法。

There can be two approaches 可以有两种方法

  1. found out $decimal value need to be used dynamically 发现$ decimal值需要动态使用
  2. Always pass maximum possible value of $decimal and remove useless 0. We can assume an upper limit of $decimal as 50. 始终传递$ decimal的最大可能值并删除无用的0.我们可以假设$ decimal的上限为50。

Please suggest if any method in php function available for any of the approach 请建议php函数中的任何方法是否适用于任何方法

I'd recommend this approach: 我推荐这种方法:

<?php


function getRidOfExtraZeros($str)
{
    // Don't want to rtrim whole numbers
    if(strpos($str,'.') === false ){ return $str; }

    // Don't want to rtrim scientific notation
    if(stripos($str,'e') !== false ){ return $str; }

    // We're good to go
    return rtrim(rtrim($str, '0'),'.');
}

$value = '0.00000000000000000000000000000000000000000000140000';

echo getRidOfExtraZeros($value),"\n"; // 0.0000000000000000000000000000000000000000000014
echo getRidOfExtraZeros('30'),"\n"; // 30
echo getRidOfExtraZeros('30.79e10'),"\n"; // 30.79e10
echo getRidOfExtraZeros('0.0'),"\n"; // 0
echo getRidOfExtraZeros('0.'),"\n"; // 0

Alternatively cast your string to float if you don't mind precision loss in edge cases and seeing some scientific notation. 或者,如果您不介意边缘情况下的精度损失并看到一些科学记数法,则将您的字符串转换为浮动。 A carefully used rtrim is probably the best approach. 精心使用的rtrim可能是最好的方法。

You set the decimals to 50, that is why it outputs the last zeros. 您将小数设置为50,这就是它输出最后的零的原因。
If you calculate what $decimals should be then it should give you what you need. 如果你计算$ decimals应该是什么,那么它应该给你你需要的东西。

$value = 0.0000000000000000000000000000000000000000000014;
// Find what the number is 
$end = str_replace(".", "", explode("E-", $value)[0]); // 14
// Find consecutive zeros to $end
Preg_match("/\.\d+" . $end ."/", number_format($value, 100), $match);
$decimal = strlen($match[0])-1; // make that number the end -1 due to the dot

Echo number_format($value, $decimal);

https://3v4l.org/fE1XD https://3v4l.org/fE1XD

It ain't pretty but it works for this. 它不漂亮,但它适用于此。

Edit: I have numberformat ($value, 100 ) because I need to be sure to get the full number as a float value. 编辑:我有numberformat($ value, 100 ),因为我需要确保将完整数字作为浮点值。
Then I use the preg_match to find the pattern [0] and what your number is on this float number. 然后我使用preg_match来查找模式[0]以及你的数字在这个浮点数上的数字。

Edit 2. This will show you why a fixed decimals is not a good solution. 编辑2.这将显示为什么固定小数不是一个好的解决方案。
Floating numbers are not precise. 浮动数字不准确。
https://3v4l.org/Ai0kE https://3v4l.org/Ai0kE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM