<?php
function parseScientificNotation($scientificString) {
// Explode the string into mantissa and exponent parts
$parts = explode('e', strtolower($scientificString));
if (count($parts) != 2) {
return "Invalid scientific notation";
}
$mantissa = $parts[0];
$exponent = (int) $parts[1];
// Determine if the number is negative
$isNegative = false;
if ($mantissa[0] == '-') {
$isNegative = true;
$mantissa = substr($mantissa, 1);
} elseif ($mantissa[0] == '+') {
$mantissa = substr($mantissa, 1);
}
// Split the mantissa into integer and fractional parts
$mantissaParts = explode('.', $mantissa);
$integerPart = $mantissaParts[0];
$fractionalPart = isset($mantissaParts[1]) ? $mantissaParts[1] : '';
// Normalize the mantissa by removing the decimal point
$mantissaNormalized = $integerPart . $fractionalPart;
// Calculate the shift based on the exponent
$shift = $exponent - strlen($fractionalPart);
if ($shift >= 0) {
// Positive exponent: add zeros to the right
$number = $mantissaNormalized . str_repeat('0', $shift);
} else {
// Negative exponent: add zeros to the left and insert the decimal point
$decimalPointPosition = strlen($integerPart) + $shift;
if ($decimalPointPosition > 0) {
$number = substr($mantissaNormalized, 0, $decimalPointPosition) . '.' . substr($mantissaNormalized, $decimalPointPosition);
} else {
$number = '0.' . str_repeat('0', -$decimalPointPosition) . $mantissaNormalized;
}
}
// Restore the sign if the number was negative
if ($isNegative) {
$number = '-' . $number;
}
return $number;
}
// Example large scientific notation string
//$scientificString = "1.23e308";
$scientificString = (string) 1 / 1000000;
// Parse and convert the string
$result = parseScientificNotation($scientificString);
echo $result;
?>
- Output for 8.1.0 - 8.1.30, 8.2.0 - 8.2.24, 8.3.0 - 8.3.12
- 0.00000010
preferences:
43.18 ms | 406 KiB | 5 Q