Surprisingly, neither ASP nor PHP seem to have a built-in way of formatting a number in scientific notation. Today we're going to change that.
ASP
function formatScientific(someFloat)
dim power
power = (someFloat mod 10) - 1
formatScientific = cStr(cDbl(someFloat) / 10^power) & "e" & cStr(power)
end function
PHP
function formatScientific($someFloat)
{
$power = ($someFloat % 10) - 1;
return ($someFloat / pow(10, $power)) . "e" . $power;
}
1 comment:
// I did not test very well, but this function works fine
function formatScientific($n) {
$exponent = floor(log10(abs($n)) + 1);
return ($n / pow(10, $exponent)) . "e" . $exponent;
}
$n = "0.12345678";
echo "formatScientific(".$n.")=" . formatScientific($n)."
";
$n = "0.000012345678";
echo "formatScientific(".$n.")=" . formatScientific($n)."
";
Post a Comment