Php- Floating Point Number Shown in Exponential Form

php- floating point number shown in exponential form

The exponential form is the internal one use by every (?) programming language (at least CPUs "sees" floats this way). Use sprintf() to format the output

echo sprintf('%f', $a);
// or (if you want to limit the number of fractional digits to lets say 6
echo sprintf('%.6f', $a);

See Manual: sprintf() about more information about the format parameter.

how to convert decimal values into exponential form in php

Take a look at sprintf and the %e and %E scientific notation formatters.

echo sprintf('%E', 0.00035074062222222);

Output

3.507406E-4

To round up your result use the optional precision specifier.

echo sprintf('.2%E', 0.00035074062222222);

Output

3.51E-4

Convert exponential number to decimal in php

You need a better math extension like BC Math, GMP... to handle the more precise precision.

Limitation of floating number & integer

round in PHP shows scientific notation instead of full number

It seems that round was the problem.
I changed it with number_format() and this does the job just fine.
Thanks Aron and Paul for the answers.

How to convert Exponentials to Decimals in PHP

EDIT: Here is some PHP magic:

$stringval = "12e-3";
$numericval = 0 + $stringval;

From the PHP docs:

If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.

If you need a more flexible format (e.g. extract four numbers from the same string), use sscanf like this:

$stringval = "12e-3";
$numericval = sscanf($stringval, "%f")[0];
echo $numericval;

PHP how to convert number exponential number to string?

printf and friends will do this:

<?php
$awb = 2.01421700079E+14;
$str = sprintf("%d", $awb);
var_dump($str);

Output:

string(15) "201421700079000"

There obviously isn't enough information in your original number to get any more precision than that.

Display float value w/o scientific notation

sprintf('%f',$ret) doesn't work, it returns 0.000000. Overflow?

sprintf works, however you miss some point here.

0.000000 is not overflow. It's just that sprintf for the %f modifier uses 6 digits per default. Also please take care that %f is locale aware, %F is probably better suited.

You might want to use more digits, e.g. let's say 4 000 000 (four million):

$ php -r "printf('%.4000000F', 1*0.000000001);"

Notice: printf(): Requested precision of 4000000 digits was truncated to PHP maximum of 53 digits in Command line code on line 1

Call Stack:
0.0001 319080 1. {main}() Command line code:0
0.0001 319200 2. printf() Command line code:1

0.00000000100000000000000006228159145777985641889706869

As this example shows, there is not only a common value (6 digits) but also a maximum (probably depended on the computer system PHP executes on), here truncated to 53 digits in my case as the warning shows.

Because of your question I'd say you want to display:

0.000000001

Which are nine digits, so you need to write it that way:

sprintf('%.9F',$ret)

However, you might want to do this:

rtrim(sprintf('%.20F', $ret), '0');

which will remove zeroes from the right afterwards:

0.000000001

Hope this is helpful.

PHP - Floating Number Precision

Because floating point arithmetic != real number arithmetic. An illustration of the difference due to imprecision is, for some floats a and b, (a+b)-b != a. This applies to any language using floats.

Since floating point are binary numbers with finite precision, there's a finite amount of representable numbers, which leads accuracy problems and surprises like this. Here's another interesting read: What Every Computer Scientist Should Know About Floating-Point Arithmetic.


Back to your problem, basically there is no way to accurately represent 34.99 or 0.01 in binary (just like in decimal, 1/3 = 0.3333...), so approximations are used instead. To get around the problem, you can:

  1. Use round($result, 2) on the result to round it to 2 decimal places.

  2. Use integers. If that's currency, say US dollars, then store $35.00 as 3500 and $34.99 as 3499, then divide the result by 100.

It's a pity that PHP doesn't have a decimal datatype like other languages do.



Related Topics



Leave a reply



Submit