<?php
function scientific2decimal($num, $decimal_separator = ".", $thousands_separator = ",")
{
if (!preg_match('!\d+(\.(\d+))?e([+-]?)(\d+)$!i', $num, $matches)) {
return $num;
}
list(,,$decimals, $sign, $exponent) = $matches;
$sign = $sign ?: "+";
$actual_decimals = strlen($decimals);
if ($sign === '+') {
$number_of_decimals = max(0, $actual_decimals - $exponent);
} else {
$number_of_decimals = $exponent + $actual_decimals;
}
return number_format($num, $number_of_decimals, $decimal_separator, $thousands_separator);
}
function number2decimal($number, $decimal_separator = ".", $thousands_separator = ",")
{
if (!is_numeric($number)) {
return $number;
}
$parts = explode('e', strtolower($number));
if (count($parts) != 2) {
return $number;
}
[$base, $exponent] = $parts;
$number_of_decimals = -$exponent + strlen($base) - strrpos($base, '.') - 1;
return number_format($number, $number_of_decimals, $decimal_separator, $thousands_separator);
}
$test = [
'aaa',
'eee',
'123ert',
7,
1e0,
0.000021,
'1e3',
'1.1337228E-3',
'1.1337228E-6',
'236.234e-5',
'1.0002E3',
'1.13372223434E+6',
'2.133333E-5',
123456789842794767576576,
];
foreach ($test as $num) {
echo $num, ": ", scientific2decimal($num), "\n";
}
echo "=============\n";
foreach ($test as $num) {
echo $num, ": ", number2decimal($num), "\n";
}
preferences:
25.81 ms | 410 KiB | 5 Q