I am reading a value from excel file when it is a number >= 14 digit it convert this to something like 3.5775004173581E+14 I want to get the exact value in decimal like 3.57750041735819 I have tried
(float) 3.5775004173581e+14 O/P 3.5775004173581e+14
intval((float) 3.5775004173581e+14) O/P 740815490
number_format(3.5775004173581e+14) O/P 3.57750041735810
but former return the same string as O/P second one produces some garbage value where as works well for the exponent <14 and the number_format adds traling 0 after 14 digits.
That's because float precision setting is default 14. To change this, use ini_set(), for example.Then you'll be able to get proper values. Sample:
$strVal = "1234567890.123456789";
//float(1234567890.1235), because
//default precision is 14:
var_dump((double)$strVal);
//float(1234567890.123456717)
ini_set('precision', 19);
var_dump((double)$strVal);
This is not only about decimal precision, but about float precision
:
$strVal = "1234567890123456789";
var_dump((double)$strVal);//float(1.2345678901235E+18)
ini_set('precision', 19);
var_dump((double)$strVal);//float(1234567890123456768)
Also, important note - it seems that trying to overcome precision in your case is an attempt to resolve symptoms, not the problem. So you should choose correct data model rather than try to solve this "problem".
Related
I need to use numbers with 16 decimal places in a project and do math operations with them, but I have problems with their handling.
Whatever it does in php, these are truncated if they are too long.
such as if:
$value = 123456789.1234567890;
Printing it with "echo" the result I have on the screen is this: 123456789.12346
Even if the number has few elements in the integer part, the decimal part is truncated
$value = 0.12345678901234567890;
In output I have:
0.12345678901235
What can I do to manage them easily?
Thanks for your help.
I tried using the instruction suggested by GuidoFaecke:
ini_set('precision', '16')
Or:
ini_set('precision', '-1');
But when I use -1 I don't see decimal number. Using 16 I don't see changing showing the number.
I have a an object like
{"latitude":-37.81425094604492, "longitude":144.96316528320312}
and I obtain
$latitude = $myobj->latitude;
but the value that is returned is -37.814250946045 , with 12 digits fractional part (rounded to 12 digits from the original 14)
how can I obtain the actual -37.81425094604492 with all 14 digits and no rounding?
It seems that you need to keep the serialization precision to 17, and this is a reasonable and feasible precision.
In php.ini , there are configuration argument precision looks like this:
; The number of significant digits displayed in floating point numbers.
precision = 14
Change it to what you want, like:
precision = 17
And the json_decode should keep the precision you need.
Hope this answer helps.
floor function in PHP behave weirdly.
For 16 decimal values it gives floor value but by increasing 1 decimal it round.
$int = 0.99999999999999999;
echo floor($int); // returns 1
$int = 0.9999999999999999;
echo floor($int); // returns 0
$int = 0.99999999999999994;
echo floor($int); // returns 0
Is it defined/explained somewhere, at which point it gives "round" value?
Is there any function which gives 0 anyhow how many 9 in decimals?
It's not floor that rounds, it's floating point math that does.
This line:
echo 0.99999999999999999;
Prints 1 (demo) because 0.99999999999999999 is too precise to be represented by a (64-bit?) float, so the closest possible value is taken, which happens to be 1.
0.99999999999999994 is also too precise to be represented exactly, but here the closest representable value happens to be 0.9999999999999999.
Is it defined/explained somewhere, at which point it gives "round" value?
It's complicated, but the numbers are rounded almost always.
I believe there is no definition of "from when values will be approximated", but that is a mathematical property that follows from the definitions in the IEEE 754 floating point standard.
To be safe, just assume everything is approximated.
Is there any function which gives 0 anyhow how many 9 in decimals?
No. The problem is that, for PHP, 0.99999999999999999 is literally the same as 1.
They're represented by exactly the same sequence of bits, so it can't distinguish them.
There are some solutions to work with bigger precision decimals, but that requires some major code changes.
Probably of interest to you:
Working with large numbers in PHP
Note that while you may get arbitrary precision, you will never get infinite precision, as that would require infinite amounts of storage.
Also note that if you actually were dealing with infinite precision, 0.999... (going on forever) would be truly (as in, mathematically provable) equal to 1, as explained in depth in this Wikipedia article.
$float_14_digits = 0.99999999999999;
echo $float_14_digits; // prints 0.99999999999999
echo floor($float_14_digits); // prints 0
$float_15_digits = 0.999999999999999;
echo $float_15_digits; // prints 1
echo floor($float_15_digits); // prints 1
exit;
on my development machine that behavior happens on digit '15' not '17' like yours. PHP rounds the last digit in the floating numbers. your floor() function has nothing to do with this behavior
Hi I'm trying to rounding a number into the 16 decimal digits but it only show and doesn't round up till 14 decimal digit.
Here's my attempt:
<?php
$num= 0.16346153846153846;
$round = number_format((float)$num, 17, '.', '');
echo $round * -1;
?>
OUTPUT:
-0.16346153846154
EXPECTED OUTPUT:
0.1634615384615385
I know that float is only 14 decimal digits. Is there any other way around for the 16 decimal digits?
You can set this parameters at run-time. 16 digits is usually the maximum
value on most platforms, larger values giving only meaningless or "fictional"
digits:
ini_set("precision", "16");
See Changing precision level floating-point variables
Recently we experienced an issue with the reading of amount(decimal number) from an excel file which has a decimal number with more than 14 digits after the decimal. The issue is fixed after using the option of an advanced algorithm for converting the decimal number. For this, we need to set,
ini_set('precision', -1);
Ref: http://php.net/precision
Can you try doing this, & see what happens? (May not be the best way of dealing with floats and numbers though)
$num = 0.16346153846153846;
$new_num = $num * -1;
echo number_format((float)$new_num, 17);
Multiply first, then try rounding the result.
number_format and the precision INI setting uses float, which is likely to result in unexpected behaviour if you're rounding to that many decimal digits.
You can alternatively use the PHP decimal extension with $decimal->toFixed(16) or $decimal->round(16) to achieve this with guaranteed accuracy regardless of your INI.
I have a question regarding number formating in PHP.
I have a variable called "average", which is simply an average of a few values. To make it clear I rounded the number to 2 decimal places. Now the problem is, that if the average is for example 2.90, it only shows 2.9. Is there any way of displaying 2 decimal places always? I though I could do it by multiplying the number by 100, rounding it to zero d.p. and then divide by 100 again, but that seems a bit overcomplicated if there is an easier way of doing it.
Maybe you can try the number_format(float $number [, int $decimals = 0 ])?
For more information, take a look at http://php.net/manual/en/function.number-format.php
Format the output with printf
printf("%.1f", $num); // prints 1 decimal place
printf("%.2f", $num); // prints 2 decimal places