note 16312 deleted from function.sprintf by jimw
| From: | jimw@php.net | Date: | Wed, 24 Oct 2001 20:44:57 +0000 |
| Subject: | note 16312 deleted from function.sprintf by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-19400@lists.php.net to get a copy of this message | ||
You missed %g for printf?
In version 2.x, printf had a %g, but in 3.x and 4.x, not. Don't know why... printf should be
made more C like.
Try this:
- - -
//
// usage:
// $out = myEE(15000000000000); -> "1.50000E13"
// $out = myEE(0.0000000000007856586); -> "7.85659E-13"
// $out = myEE(2.123456789); -> "2.12346E00"
// $out = myEE(0); -> "0.00000E00"
// $out = myEE(-1.234); -> "-1.23400E00"
//
function myEE($_in) {
$local_debug = 0;
if ( $local_debug > 0 ) {
echo("_in: $_in\n");
}
$in = $_in + 0.0; // force php to convert string to number
if ( $in == 0 ) {
$decimals_out = "0";
} else {
$decimals = log10( abs($in) );
$decimals_floor = floor($decimals);
if ( $decimals_floor < 0 ) {
$decimals_cut = sprintf("-%02d", abs($decimals_floor)); // 7 -> 07, 12 ->
12, 1234 -> 1234
} else {
$decimals_cut = sprintf("%02d", $decimals_floor); // 7 -> 07, 12 -> 12,
1234 -> 1234
}
$decimals_out = $decimals_cut;
if ( $local_debug > 0 ) {
echo(" decimals: $decimals // after log10 and abs\n");
echo(" decimals_floor: $decimals_floor // after floor 12.5912 -> 12\n");
echo(" decimals_cut: $decimals_cut // after cut\n");
echo(" decimals_out: $decimals_out // \n");
}
}
//
// -- now, $decimals_out is ready
//
//echo(" decimals_pow: " . pow(10, $decimals_out) . " // pow(10,
decimals)\n");
$mant = $in / pow(10, $decimals_out);
$mant = sprintf("%.5f", $mant); // |n.nnnnn|
$out = "${mant}E$decimals_out"; // format output string |[-]n.nnnnnE[-]nn|
if ( $local_debug > 0 ) {
print(" mant: $mant\n");
print(" out: $out\n");
//$mant2 = sprintf("%e", $mant);
//print(" mant2: $mant2 \n");
}
return($out);
}
- - -