Why is PHP printing my number in scientific notation, when I specified it as .000021?

Viewed 54799

In PHP I have the following code:

<?PHP
  $var = .000021;
  echo $var;
?>

the output is 2.1E-5 !

Why? it should print .000021

8 Answers

The previous answers responded to OP question, but none offered the code to do it.
Use this function to format any number with E- format.

function format_amount_with_no_e($amount) {
    $amount = (string)$amount; // cast the number in string
    $pos = stripos($amount, 'E-'); // get the E- position
    $there_is_e = $pos !== false; // E- is found

    if ($there_is_e) {
        $decimals = intval(substr($amount, $pos + 2, strlen($amount))); // extract the decimals
        $amount = number_format($amount, $decimals, '.', ','); // format the number without E-
    }

    return $amount;
}

Please note the function will always return a string.

for Float you can use

$var = '122.34343The';
echo floatval($var); // print 122.34343

for int you can use

echo intval(42000000); // 42000000
Related