<?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";
}
- Output for 8.4.1 - 8.4.13
- aaa: aaa
eee: eee
123ert: 123ert
7: 7
1: 1
2.1E-5: 0.000021
1e3: 1,000
1.1337228E-3: 0.0011337228
1.1337228E-6: 0.0000011337228
236.234e-5: 0.00236234
1.0002E3: 1,000.2
1.13372223434E+6: 1,133,722.23434
2.133333E-5: 0.00002133333
1.2345678984279E+23: 123,456,789,842,794,775,576,576
=============
aaa: aaa
eee: eee
123ert: 123ert
7: 7
1: 1
2.1E-5: 0.000021
1e3: 1,000
1.1337228E-3: 0.0011337228
1.1337228E-6: 0.0000011337228
236.234e-5: 0.00236234
1.0002E3: 1,000.2
1.13372223434E+6: 1,133,722.23434
2.133333E-5: 0.00002133333
1.2345678984279E+23: 123,456,789,842,789,994,070,016
- Output for 8.3.0 - 8.3.26
- aaa: aaa
eee: eee
123ert: 123ert
7: 7
1: 1
2.1E-5: 0.000021
1e3: 1,000
1.1337228E-3: 0.0011337228
1.1337228E-6: 0.0000011337228
236.234e-5: 0.00236234
1.0002E3: 1,000.2
1.13372223434E+6: 1,133,722.23434
2.133333E-5: 0.00002133333
1.2345678984279E+23: 123,456,789,842,794,775,576,576
=============
aaa: aaa
eee: eee
123ert: 123ert
7: 7
1: 1
2.1E-5: 0.000021
1e3: 1,000
1.1337228E-3: 0.0011337228
1.1337228E-6: 0.0000011337228
236.234e-5: 0.00236234
1.0002E3: 1,000.2
1.13372223434E+6: 1,133,722.23434
2.133333E-5: 0.00002133333
1.2345678984279E+23: 123,456,789,842,799,993,290,752
- Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29
- aaa: aaa
eee: eee
123ert: 123ert
7: 7
1: 1
2.1E-5: 0.000021
1e3: 1,000
1.1337228E-3: 0.0011337228
1.1337228E-6: 0.0000011337228
236.234e-5: 0.00236234
1.0002E3: 1,000.2
1.13372223434E+6: 1,133,722.23434
2.133333E-5: 0.00002133333
1.2345678984279E+23: 123,456,789,842,794,775,576,576
=============
aaa: aaa
eee: eee
123ert: 123ert
7: 7
1: 1
2.1E-5: 0.000021
1e3: 1,000
1.1337228E-3: 0.0011337228
1.1337228E-6: 0.0000011337228
236.234e-5: 0.00236234
1.0002E3: 1,000.2
1.13372223434E+6: 1,133,722.23434
2.133333E-5: 0.00002133333
1.2345678984279E+23: 123,456,789,842,794,775,576,576
preferences:
62.75 ms | 411 KiB | 5 Q