Perfectly round a number in PHP - php

So, i retrieved with an API a number, which is 1441971834999104426.
I would like to round it to 1441971835
However, when i do : round(1441971834999104426, -9); the output is : 1.441971835E+18, which is not what i want at all.
Can you give me some advices on how I can do the rounding perfectly?
Thanks to you.

Divide the number by 1e9, and round that.
echo round(1441971834999104426/1e9);

Related

A simple calculation in php.. I'm doing it wrong?

I do not understand php:
echo -0.01-0.02-0.16+0.01+0.01+0.17;
result 2.7755575615629E-17
correctly = 0 !
E-17 really means x 10 ^ (-17).
According to the computer, which suffers from precision errors at the far end of decimal floating point numbers, it is calculating your answer to be 0.00000000000000002775557...
If you don't need that sort of precision, you can force rounding to a certain accuracy:
echo round(-0.01-0.02-0.16+0.01+0.01+0.17, 8);
The answer you got is accurate answer according to BODMAS rule if you want round up answer than just use round() function of PHP. You will get you answer.
echo round(-0.01-0.02-0.16+0.01+0.01+0.17, 8);

Set precision for floats in php for 15 numbers after comma

Servus,
I have a problem with float numbers in PHP. My number is
$cosVQ = 0.907424504992097
but when I do some math operations $cosVQ, I will receive different results in Javascript and PHP. Or for example wen I do echo I will receive only 0.9074245049920. What am I doing wrong?
You could use number_format:
echo number_format($cosVQ, 15);
Ok I found answer for my question, which works as I wanted: ini_set('precision', 15);

PHP very small decimals result in error

PHP is erroring out on me when working with small decimals / floats. Take the following code:
$spotPrices['entry'] = 1.6591;
$price['o'] = 1.65908;
$currentresult = $spotPrices['entry'] - $price['o'];
echo $currentresult;
I would expect this to output 0.00002 (the answer). But instead it outputs: -1.99999999999E-5
Why is it doing this and, more importantly, how can I get the correct result?
I've done some searching on the forums and seen that floating points give PHP fits but haven't seen a solution or workaround that seems to answer my question.
My calculator is saying that the result should be 0.00002
use number_format:
$currentresult = number_format($spotPrices['entry'] - $price['o'], 8);
Instead of 0.00002 you get 1.9999999999909E-5 which is 0.000019999999999909. This is due to floating point precision. Precision is platform-dependent. You can read up on it here: http://www.php.net/manual/en/language.types.float.php

Convert 6.5e-7 to a formatted number such as 0.00000060

I have a number that is pulled from an API that displays as 6.5e-7, I would like to display this as 0.00000060 (I think this is what 6.5e-7 means),
I have tried to get this correct using the php ROUND function but it will only show as 0 or 6.5e-7 when I used a precision.
print round($vSat, 8);
Any help would be much appreciated
If I had to guess, I would think you need to do a formatted print which is printf
printf ("%.8f", $vSat)
%.8f means floating point and 8 decimal places
http://us3.php.net/printf
Use printf/sprintf formatting.
printf("%10.10f", $yournumber);

Silly, can't figure out php round down :(

I have small problem and it might be silly somewhere, but still i have it :)
So the problem is:
By doing this
round(615.36*0.10, 2, PHP_ROUND_HALF_DOWN);
I expect outcome to be 61.53, but it's 61.54.
phpVersion = 5.3.2
Could anyone help me to solve this?
Thanks.
PHP_ROUND_HALF_DOWN will round the half -- i.e. the 0.005 part.
if you have 61.535, using PHP_ROUND_HALF_DOWN will get you 61.53 -- instead of the 61.54 you should have obtained with usual rounding.
Basicall, the .005 half has been rounded down.
But 61.536 is not a half : .006 is more than .005 ; so rounding that value gives 61.54.
In your case, you could multiply the value by 100, use the floor() function, and divide the result by 100 -- I suppose it would give you what you expect :
$value = 61.536;
$value_times_100 = $value * 100;
$value_times_100_floored = floor($value_times_100);
$value_floored = $value_times_100_floored / 100;
var_dump($value_floored);
Gives me :
float(61.53)
If you want to round down, you'll need to use floor(), which doesn't have a means to specify precision, so it has to be worked around, eg. with
function floor_prec($x, $prec) {
return floor($x*pow(10,$prec))/pow(10,$prec);
}
you obviously only wanting it to two decimal places why not just number_format(615.36*0.10, 2)

Categories