<?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 git.master, git.master_jit, rfc.property-hooks
- 0.00000010
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
29.54 ms | 405 KiB | 5 Q