<?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];
// Handle cases where the exponent is zero
if ($exponent == 0) {
return $mantissa;
}
// 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] : '';
// Calculate the effective length of the mantissa
$mantissaLength = strlen($integerPart) + strlen($fractionalPart);
// Normalize the mantissa by removing the decimal point
$mantissaNormalized = $integerPart . $fractionalPart;
// Calculate the shift based on the exponent
$shift = $exponent + strlen($fractionalPart);
// Handle positive and negative exponent cases
if ($shift >= 0) {
$number = $mantissaNormalized . str_repeat('0', $shift - strlen($mantissaNormalized));
} else {
$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";
// 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
- 1230000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
preferences:
46.75 ms | 407 KiB | 5 Q